Gridview FindControl的使用

//先看看FindControl的源碼
protected
virtual Control FindControl(string id, int pathOffset) { string str; this.EnsureChildControls(); if (!this.flags[0x80]) { Control namingContainer = this.NamingContainer; if (namingContainer != null) { return namingContainer.FindControl(id, pathOffset);//向上遞歸 } return null; } if (this.HasControls() && (this._occasionalFields.NamedControls == null)) { this.EnsureNamedControlsTable(); } if ((this._occasionalFields == null) || (this._occasionalFields.NamedControls == null)) { return null; } char[] anyOf = new char[] { '$', ':' };//name,id int num = id.IndexOfAny(anyOf, pathOffset); if (num == -1)//不再存在父控件(容器)時,獲得該查找ID的控件 { str = id.Substring(pathOffset); return (this._occasionalFields.NamedControls[str] as Control); } str = id.Substring(pathOffset, num - pathOffset);//否則,繼續尋找父控件(容器),調用其FindControl方法 Control control2 = this._occasionalFields.NamedControls[str] as Control; if (control2 == null) { return null; } return control2.FindControl(id, num + 1);//pathOffset往上一級 }
//再看GridView列
    <asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound" Width="1680px">
        <Columns>           
            <asp:TemplateField HeaderText="編碼">
                <ItemTemplate>
                    <asp:Literal ID="Literal1" runat="server" Text='<%# eval_r("ID") %>' Visible="false"></asp:Literal>
                    <asp:HyperLink ID="HyperLink1" runat="server"
                        NavigateUrl='<%# eval_r("ID", "Edit.aspx?Id={0}") %>' Text='<%# eval_r("ID") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="FindDeptName" HeaderText="發現單位" >
                <ItemStyle Width="80px" />
            </asp:BoundField>
            <asp:BoundField DataField="FindUserName" HeaderText="發現人" >   
                <ItemStyle Width="50px" />
            </asp:BoundField>
            <asp:BoundField DataField="FindWay" HeaderText="發現方式" >               
                <ItemStyle Width="60px" />
            </asp:BoundField>
            <asp:BoundField HeaderText="所在裝置" DataField="SZZZ" >
                <ItemStyle Width="90px" />
            </asp:BoundField>
            <asp:BoundField HeaderText="隱患內容" DataField="YHNR" >
                <ItemStyle HorizontalAlign="Left" Width="280px" />
            </asp:BoundField>
            <asp:BoundField HeaderText="所屬專業" DataField="SSZY" >
                <ItemStyle Width="60px" />
            </asp:BoundField>
            <asp:BoundField HeaderText="類別" DataField="Kind" >
                <ItemStyle Width="40px" />
            </asp:BoundField>
            <asp:BoundField HeaderText="危害后果" DataField="WHHG" >
                <ItemStyle Width="60px" />
            </asp:BoundField>
            <asp:BoundField HeaderText="整改措施" DataField="ZGCS" >
                <ItemStyle HorizontalAlign="Left" Width="220px" />
            </asp:BoundField>
            <asp:BoundField HeaderText="整改責任人" DataField="ZGFZR" >
            <ItemStyle Width="80px" />
            </asp:BoundField>    
            <asp:BoundField HeaderText="計劃完成時間" DataField="PLANDATE" HtmlEncode="false" DataFormatString="{0:d}" >
            <ItemStyle Width="80px" />
            </asp:BoundField>    
            <asp:BoundField HeaderText="整改完成時間" DataField="FINISHDATE" HtmlEncode="false" DataFormatString="{0:d}" >
            <ItemStyle Width="80px" />
            </asp:BoundField>    
            <asp:TemplateField HeaderText="監督人驗收">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# eval_r("JDR")%>'></asp:Label>
                    <asp:LinkButton ID="lbYS" runat="server" Text="驗收" CommandName="Submit"
                        CommandArgument='<%#eval_r("ID") %>'
                        Visible='<%#Bll.SessionManage.IsYSRole && (DateTime.Parse(eval_r("PLANDATE").ToString()) >=DateTime.Now.Date) && eval_r("JDR").ToString()==""?true:false %>' oncommand="lbYS_Command"></asp:LinkButton>
                </ItemTemplate>               
            </asp:TemplateField>
            <asp:TemplateField HeaderText="驗收狀態">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# (DateTime.Parse(eval_r("PLANDATE").ToString()) <DateTime.Now.Date) && (eval_r("JDR").ToString() =="")?"已列入考核":"" %>' ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="確認提交">
                <ItemTemplate>
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#eval_r("WTKZT") %>' />
                    <asp:LinkButton ID="lbTJ" runat="server" Text="提交" CommandName="Submit"
                        CommandArgument='<%#eval_r("ID") %>' oncommand="LinkButton1_Command" Visible='<%#eval_r("WTKZT").ToString()==""?true:false %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
//最后看GridView的一行html源碼
<tr align="center" onmousemove="this.style.backgroundColor='#EDF7F8'" onmouseout="this.style.backgroundColor='#ffffff'">
<td>20100426112155</td>
<td style="width:80px;">動力</td>
<td style="width:50px;">管理員</td>
<td style="width:60px;">監控</td>
<td style="width:90px;">2#煤氣化</td>
<td align="left" style="width:280px;">a</td>
<td style="width:60px;">設備</td>
<td style="width:40px;">設備</td>
<td style="width:60px;">輕</td>
<td align="left" style="width:220px;">&nbsp;</td>
<td style="width:80px;">&nbsp;</td>
<td style="width:80px;">2010-5-11</td>
<td style="width:80px;">2010-5-11</td>
<td><span id="GridView1_ctl03_Label2"></span></td>
<td><span id="GridView1_ctl03_Label1"></span></td>
<td><input type="hidden" name="GridView1$ctl03$HiddenField1" id="GridView1_ctl03_HiddenField1" value="1" />
</td>
</tr>
結論:
當使用Cell[n].FindControl方法時,是從最低一級開始查,如Cell[n].FindControl("HiddenField1")
然后Cell會自動調用Rows[n].FindControl("ctl03$HiddenField1"),pathOffset加1,因為每行控件名稱都不一樣,這是自動
將控件名稱補全,pathOffset再加1,最后調用GridView.FindControl("GridView1$ctl03$HiddenField1")
所以我們使用Cell[1]和Cell[20]的FindControl方法時,返回結果是一樣的。
但是Rows[1]和Rows[20]的FindControl方法時,返回結果是不一樣的。因為向上級反映的ctl(n)是不一樣的。
 
例子:上述控件的部分代碼
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //超期、未驗收 紅色停止、已列入考核
            if ((e.Row.Cells[14].FindControl("Label1") as Label).Text == "已列入考核")
            {
                e.Row.Attributes.Clear();
                e.Row.Attributes["style"] = "background-color:red; color:white;";
                HyperLink hl = (e.Row.Cells[0].FindControl("HyperLink1") as HyperLink);
                if (hl !=null)
                {
                    hl.Attributes["style"] = "color:white";
                }
                LinkButton lb = (e.Row.Cells[15].FindControl("lbTJ") as LinkButton);
                if (lb != null)
                {
                    lb.Attributes["style"] = "color:white";
                   
            }
            //已驗收 綠色通過
            if ((e.Row.Cells[13].FindControl("Label2") as Label).Text != "")
            {
                e.Row.Attributes.Clear();
                e.Row.Attributes["style"] = "background-color:#00A600; color:white;";
                HyperLink hl = (e.Row.Cells[0].FindControl("HyperLink1") as HyperLink);
                if (hl != null)
                {
                    hl.Attributes["style"] = "color:white";
                }
                LinkButton lb = (e.Row.Cells[15].FindControl("lbTJ") as LinkButton);
                if (lb != null)
                {
                    lb.Attributes["style"] = "color:white";
                }
            }
        }
    }
可以更改為如下代碼,好處是不用再挨個去數某個單元格,然后調用其FindControl方法了。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //超期、未驗收 紅色停止、已列入考核
            if ((e.Row.FindControl("Label1") as Label).Text == "已列入考核")
            {
                e.Row.Attributes.Clear();
                e.Row.Attributes["style"] = "background-color:red; color:white;";
                HyperLink hl = (e.Row.FindControl("HyperLink1") as HyperLink);
                if (hl !=null)
                {
                    hl.Attributes["style"] = "color:white";
                }
                LinkButton lb = (e.Row.FindControl("lbTJ") as LinkButton);
                if (lb != null)
                {
                    lb.Attributes["style"] = "color:white";
                   
            }
            //已驗收 綠色通過
            if ((e.Row.FindControl("Label2") as Label).Text != "")
            {
                e.Row.Attributes.Clear();
                e.Row.Attributes["style"] = "background-color:#00A600; color:white;";
                HyperLink hl = (e.Row.FindControl("HyperLink1") as HyperLink);
                if (hl != null)
                {
                    hl.Attributes["style"] = "color:white";
                }
                LinkButton lb = (e.Row.FindControl("lbTJ") as LinkButton);
                if (lb != null)
                {
                    lb.Attributes["style"] = "color:white";
                }
            }
        }
    }