Gridview FindControl的使用

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";

                }

            }

        }

    }

 

源文档 <http://www.blogjava.net/liuyz2006/articles/378622.html>

posted @ 2013-11-27 00:04  weifb  阅读(792)  评论(0)    收藏  举报