关于GridView中控件的问题

最近做项目报表时,会遇到在Gridview中有一些控件,报表中也会有更新、删除等一系列的操作,但往往会遇到一些控件取值取不到或者找不到控件得问题,通过网上查阅资料对其中的一些做一总结:

前台代码如下:

  1   <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" CssClass="GridViewStyle"
  2     DataKeyNames="ID"  OnRowEditing="gv_RowEditing" OnRowCancelingEdit="gv_RowCancelingEdit"
  4     OnRowUpdating="gv_RowUpdating" OnRowDataBound="gv_RowDataBound">
  5     <FooterStyle CssClass="GridViewFooterStyle" Font-Bold="False" />
  6                         <RowStyle CssClass="GridViewRowStyle" />
  7                         <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
  8                         <PagerStyle CssClass="GridViewPagerStyle" />
  9                         <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
 10                         <HeaderStyle CssClass="GridViewHeaderStyle" />
 11                         <EmptyDataTemplate>
 12                             <asp:Table ID="Table1" runat="server" CellPadding="-1" CellSpacing="0" CssClass="GridViewHeaderStyle"
 13                                 GridLines="Vertical">
 14                                 <asp:TableRow>
 15                                     <asp:TableCell HorizontalAlign="center" Text="QAD" Width="40px"></asp:TableCell>
 18                                     <asp:TableCell HorizontalAlign="center" Text="单位" Width="50px"></asp:TableCell>
 20                                     <asp:TableCell HorizontalAlign="center" Text="物料描述1" Width="120px"></asp:TableCell>
 23                                     <asp:TableCell HorizontalAlign="center" Text="描述" Width="120px"></asp:TableCell>
 24                                     <asp:TableCell HorizontalAlign="center" Text="数量" Width="60px"></asp:TableCell>
 26                                 </asp:TableRow>
 27                             </asp:Table>
 28                         </EmptyDataTemplate>
 29                         <Columns>
 30                             <asp:TemplateField HeaderText="QAD" >
 31                                 <EditItemTemplate>
 32                                 <%--<asp:Label ID="txtQAD" runat="server" CssClass="cssQAD"  Text='<%# Bind("rp_QAD") %>'></asp:Label>--%>
 33                                 <asp:TextBox ID="txtQAD" runat="server" AutoComplete="Off" CssClass="SmallTextBox CCPPart cssQAD"  Text='<%# Bind("rp_QAD") %>'
 34                                     Width="100px"></asp:TextBox>
 35                                 </EditItemTemplate>
 36                                 <ItemStyle HorizontalAlign="Center" />
 37                                 <HeaderStyle HorizontalAlign="Center" Width="100px" />
 38                                 <ItemTemplate>
 39                                     <%#Eval("rp_QAD")%>
 40                                 </ItemTemplate>
 41                             </asp:TemplateField>
 76                             <asp:TemplateField HeaderText="基本单位">
 77                                 <EditItemTemplate>
 78                                 <asp:Label ID="txtPtUm" runat="server" CssClass="cssPtUm" Text='<%# Bind("rp_PtUm") %>'></asp:Label>
 81                                 </EditItemTemplate>
 82                                 <ItemStyle HorizontalAlign="Center" />
 83                                 <HeaderStyle HorizontalAlign="Center" Width="30px" />
 84                                 <ItemTemplate>
 85                                     <%#Eval("rp_PtUm")%>
 86                                 </ItemTemplate>
 87                             </asp:TemplateField>
 88                             <asp:TemplateField HeaderText="实际采购单位" >
 89                                 <EditItemTemplate>
 90                                     <asp:DropDownList ID="ddlUm" Width="35px" runat="server" DataTextField="pc_um" >
 91                                     </asp:DropDownList>
 92                                     <asp:TextBox ID="txtUmOther" Width="35px" runat="server"></asp:TextBox>
 93                                 </EditItemTemplate>
 94                                 <ItemStyle HorizontalAlign="Center" />
 95                                 <HeaderStyle HorizontalAlign="Center" Width="70px" />
 96                                 <ItemTemplate>
 97                                     <asp:Label ID="labUm" runat="server" Text='<%# Bind("rp_Um") %>'></asp:Label>
 99                                 </ItemTemplate>
100                             </asp:TemplateField>
114                             <asp:TemplateField HeaderText="实际数量">
115                                 <EditItemTemplate>
116                                 <asp:TextBox ID="txtQty" runat="server" CssClass="SmallTextBox cssQty" Text='<%# Bind("rp_Qty") %>'
117                                     Width="40px"></asp:TextBox>
118                                 </EditItemTemplate>
119                                 <ItemStyle HorizontalAlign="Center" />
120                                 <HeaderStyle HorizontalAlign="Center" Width="40px" />
121                                 <ItemTemplate>
122                                     <%#Eval("rp_Qty")%>
123                                 </ItemTemplate>
124                             </asp:TemplateField>
137                             <asp:TemplateField HeaderText="物料描述1">
138                                 <EditItemTemplate>
139                                 <asp:Label ID="txtQADDesc1" runat="server" CssClass="cssQADDesc1" Text='<%# Bind("rp_QADDesc1") %>'></asp:Label>
140                                 </EditItemTemplate>
143                                 <ItemStyle HorizontalAlign="Center" />
144                                 <HeaderStyle HorizontalAlign="Center" Width="100px" />
145                                 <ItemTemplate>
146                                     <%#Eval("rp_QADDesc1")%>
147                                 </ItemTemplate>
148                             </asp:TemplateField>
165                             <asp:BoundField DataField="rp_Descript" HeaderText="描述" ReadOnly="True">
166                                 <HeaderStyle Width="100px" HorizontalAlign="Right" Font-Bold="False" />
167                                 <ItemStyle Width="100px" HorizontalAlign="Right" />
168                             </asp:BoundField>
180                             <asp:CommandField ShowEditButton="True" CancelText="<u>取消</u>" DeleteText="<u>删除</u>"
181                                 EditText="<u>编辑</u>" UpdateText="<u>更新</u>">
182                                 <HeaderStyle Width="70px" HorizontalAlign="Center" />
183                                 <ItemStyle HorizontalAlign="Center" />
184                                 <ControlStyle Font-Bold="False" Font-Size="12px" />
185                             </asp:CommandField>
186                         </Columns>
187                     </asp:GridView>

 更新和绑定时时获取控件的值要根据每一行的index,然后根据控件的ID值获取到对应控件的值

后台代码如下:

  1     protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
  2     {
  7         string um = ((DropDownList)gv.Rows[e.RowIndex].Cells[6].FindControl("ddlUm")).Text.ToString().Trim();
  8         string qaddesc1 = ((Label)gv.Rows[e.RowIndex].Cells[8].FindControl("txtQADDesc1")).Text.ToString().Trim();
  9         string qty = ((TextBox)gv.Rows[e.RowIndex].Cells[7].FindControl("txtQty")).Text.ToString().Trim();
 13         .......
154 }
179 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 180 { 181 if (e.Row.RowType == DataControlRowType.DataRow) 182 { 183 string qad= ((TextBox)e.Row.FindControl("txtQAD")).ToString();
188 string dum = ((DropDownList)e.Row.FindControl("ddlUm")).ToString();
198 string lum = ((Label)e.Row.FindControl("labUm")).Text.ToString(); 218 ....... 222 } 223 }

 以上只是在做的过程中遇到的,在以后遇到类似的问题会进行补充更新;

 不足之处多多指教,如有雷同请联系楼主!

点滴积累,汇聚成河
原文地址:https://www.cnblogs.com/ABblog/p/5718499.html