zoukankan      html  css  js  c++  java
  • GRIDVIEW FINDCONTROL的使用

      1 GRIDVIEW FINDCONTROL的使用
      2 Gridview FindControl的使用
      3 //先看看FindControl的源码
      4 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往上一级 }
      5 //再看GridView列
      6     <asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound" Width="1680px">
      7         <Columns>            
      8             <asp:TemplateField HeaderText="编码">
      9                 <ItemTemplate>
     10                     <asp:Literal ID="Literal1" runat="server" Text='<%# eval_r("ID") %>' Visible="false"></asp:Literal>
     11                     <asp:HyperLink ID="HyperLink1" runat="server" 
     12                         NavigateUrl='<%# eval_r("ID", "Edit.aspx?Id={0}") %>' Text='<%# eval_r("ID") %>'></asp:HyperLink>
     13                 </ItemTemplate>
     14             </asp:TemplateField>
     15             <asp:BoundField DataField="FindDeptName" HeaderText="发现单位" >
     16                 <ItemStyle Width="80px" />
     17             </asp:BoundField>
     18             <asp:BoundField DataField="FindUserName" HeaderText="发现人" >    
     19                 <ItemStyle Width="50px" />
     20             </asp:BoundField>
     21             <asp:BoundField DataField="FindWay" HeaderText="发现方式" >                
     22                 <ItemStyle Width="60px" />
     23             </asp:BoundField>
     24             <asp:BoundField HeaderText="所在装置" DataField="SZZZ" >
     25                 <ItemStyle Width="90px" />
     26             </asp:BoundField>
     27             <asp:BoundField HeaderText="隐患内容" DataField="YHNR" >
     28                 <ItemStyle HorizontalAlign="Left" Width="280px" />
     29             </asp:BoundField>
     30             <asp:BoundField HeaderText="所属专业" DataField="SSZY" >
     31                 <ItemStyle Width="60px" />
     32             </asp:BoundField>
     33             <asp:BoundField HeaderText="类别" DataField="Kind" >
     34                 <ItemStyle Width="40px" />
     35             </asp:BoundField>
     36             <asp:BoundField HeaderText="危害后果" DataField="WHHG" >
     37                 <ItemStyle Width="60px" />
     38             </asp:BoundField>
     39             <asp:BoundField HeaderText="整改措施" DataField="ZGCS" >
     40                 <ItemStyle HorizontalAlign="Left" Width="220px" />
     41             </asp:BoundField>
     42             <asp:BoundField HeaderText="整改责任人" DataField="ZGFZR" >
     43             <ItemStyle Width="80px" />
     44             </asp:BoundField>     
     45             <asp:BoundField HeaderText="计划完成时间" DataField="PLANDATE" HtmlEncode="false" DataFormatString="{0:d}" >
     46             <ItemStyle Width="80px" />
     47             </asp:BoundField>     
     48             <asp:BoundField HeaderText="整改完成时间" DataField="FINISHDATE" HtmlEncode="false" DataFormatString="{0:d}" >
     49             <ItemStyle Width="80px" />
     50             </asp:BoundField>     
     51             <asp:TemplateField HeaderText="监督人验收">
     52                 <ItemTemplate>
     53                     <asp:Label ID="Label2" runat="server" Text='<%# eval_r("JDR")%>'></asp:Label>
     54                     <asp:LinkButton ID="lbYS" runat="server" Text="验收" CommandName="Submit" 
     55                         CommandArgument='<%#eval_r("ID") %>' 
     56                         Visible='<%#Bll.SessionManage.IsYSRole && (DateTime.Parse(eval_r("PLANDATE").ToString()) >=DateTime.Now.Date) && eval_r("JDR").ToString()==""?true:false %>' oncommand="lbYS_Command"></asp:LinkButton>
     57                 </ItemTemplate>                
     58             </asp:TemplateField>
     59             <asp:TemplateField HeaderText="验收状态">
     60                 <ItemTemplate>
     61                     <asp:Label ID="Label1" runat="server" Text='<%# (DateTime.Parse(eval_r("PLANDATE").ToString()) <DateTime.Now.Date) && (eval_r("JDR").ToString() =="")?"已列入考核":"" %>' ></asp:Label>
     62                 </ItemTemplate>
     63             </asp:TemplateField>
     64             <asp:TemplateField HeaderText="确认提交">
     65                 <ItemTemplate>
     66                     <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#eval_r("WTKZT") %>' />
     67                     <asp:LinkButton ID="lbTJ" runat="server" Text="提交" CommandName="Submit"
     68                         CommandArgument='<%#eval_r("ID") %>' oncommand="LinkButton1_Command" Visible='<%#eval_r("WTKZT").ToString()==""?true:false %>'></asp:LinkButton>
     69                 </ItemTemplate>
     70             </asp:TemplateField>
     71         </Columns>
     72     </asp:GridView>
     73 //最后看GridView的一行html源码
     74 <tr align="center" onmousemove="this.style.backgroundColor='#EDF7F8'" onmouseout="this.style.backgroundColor='#ffffff'">
     75 <td>20100426112155</td>
     76 <td style="80px;">动力</td>
     77 <td style="50px;">管理员</td>
     78 <td style="60px;">监控</td>
     79 <td style="90px;">2#煤气化</td>
     80 <td align="left" style="280px;">a</td>
     81 <td style="60px;">设备</td>
     82 <td style="40px;">设备</td>
     83 <td style="60px;">轻</td>
     84 <td align="left" style="220px;">&nbsp;</td>
     85 <td style="80px;">&nbsp;</td>
     86 <td style="80px;">2010-5-11</td>
     87 <td style="80px;">2010-5-11</td>
     88 <td><span id="GridView1_ctl03_Label2"></span></td>
     89 <td><span id="GridView1_ctl03_Label1"></span></td>
     90 <td><input type="hidden" name="GridView1$ctl03$HiddenField1" id="GridView1_ctl03_HiddenField1" value="1" />
     91 </td>
     92 </tr>
     93 结论:
     94 当使用Cell[n].FindControl方法时,是从最低一级开始查,如Cell[n].FindControl("HiddenField1")
     95 然后Cell会自动调用Rows[n].FindControl("ctl03$HiddenField1"),pathOffset加1,因为每行控件名称都不一样,这是自动
     96 将控件名称补全,pathOffset再加1,最后调用GridView.FindControl("GridView1$ctl03$HiddenField1")
     97 所以我们使用Cell[1]和Cell[20]的FindControl方法时,返回结果是一样的。
     98 但是Rows[1]和Rows[20]的FindControl方法时,返回结果是不一样的。因为向上级反映的ctl(n)是不一样的。
     99  
    100 例子:上述控件的部分代码
    101 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    102     {
    103         if (e.Row.RowType == DataControlRowType.DataRow)
    104         {
    105             //超期、未验收 红色停止、已列入考核
    106             if ((e.Row.Cells[14].FindControl("Label1") as Label).Text == "已列入考核")
    107             {
    108                 e.Row.Attributes.Clear();
    109                 e.Row.Attributes["style"] = "background-color:red; color:white;";
    110                 HyperLink hl = (e.Row.Cells[0].FindControl("HyperLink1") as HyperLink);
    111                 if (hl !=null)
    112                 {
    113                     hl.Attributes["style"] = "color:white";
    114                 }
    115                 LinkButton lb = (e.Row.Cells[15].FindControl("lbTJ") as LinkButton);
    116                 if (lb != null)
    117                 {
    118                     lb.Attributes["style"] = "color:white";
    119                 }      
    120             }
    121             //已验收 绿色通过
    122             if ((e.Row.Cells[13].FindControl("Label2") as Label).Text != "")
    123             {
    124                 e.Row.Attributes.Clear();
    125                 e.Row.Attributes["style"] = "background-color:#00A600; color:white;";
    126                 HyperLink hl = (e.Row.Cells[0].FindControl("HyperLink1") as HyperLink);
    127                 if (hl != null)
    128                 {
    129                     hl.Attributes["style"] = "color:white";
    130                 }
    131                 LinkButton lb = (e.Row.Cells[15].FindControl("lbTJ") as LinkButton);
    132                 if (lb != null)
    133                 {
    134                     lb.Attributes["style"] = "color:white";
    135                 } 
    136             }
    137         }
    138     }
    139 可以更改为如下代码,好处是不用再挨个去数某个单元格,然后调用其FindControl方法了。
    140 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    141     {
    142         if (e.Row.RowType == DataControlRowType.DataRow)
    143         {
    144             //超期、未验收 红色停止、已列入考核
    145             if ((e.Row.FindControl("Label1") as Label).Text == "已列入考核")
    146             {
    147                 e.Row.Attributes.Clear();
    148                 e.Row.Attributes["style"] = "background-color:red; color:white;";
    149                 HyperLink hl = (e.Row.FindControl("HyperLink1") as HyperLink);
    150                 if (hl !=null)
    151                 {
    152                     hl.Attributes["style"] = "color:white";
    153                 }
    154                 LinkButton lb = (e.Row.FindControl("lbTJ") as LinkButton);
    155                 if (lb != null)
    156                 {
    157                     lb.Attributes["style"] = "color:white";
    158                 }      
    159             }
    160             //已验收 绿色通过
    161             if ((e.Row.FindControl("Label2") as Label).Text != "")
    162             {
    163                 e.Row.Attributes.Clear();
    164                 e.Row.Attributes["style"] = "background-color:#00A600; color:white;";
    165                 HyperLink hl = (e.Row.FindControl("HyperLink1") as HyperLink);
    166                 if (hl != null)
    167                 {
    168                     hl.Attributes["style"] = "color:white";
    169                 }
    170                 LinkButton lb = (e.Row.FindControl("lbTJ") as LinkButton);
    171                 if (lb != null)
    172                 {
    173                     lb.Attributes["style"] = "color:white";
    174                 } 
    175             }
    176         }
    177     }
  • 相关阅读:
    【项目】项目41
    【项目】项目40
    【项目】项目39
    【项目】项目38
    【项目】项目37
    15-155. Min Stack
    14-160. Intersection of Two Linked Lists
    13-169. Majority Element
    12-206. Reverse Linked List
    11-215. Kth Largest Element in an Array
  • 原文地址:https://www.cnblogs.com/YoungPop-Chen/p/3312907.html
Copyright © 2011-2022 走看看