先看前台代码
<asp:GridView runat="server" ID="GridView1" CssClass="Result_tab" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="id" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText="有效时间" SortExpression="audit"> <EditItemTemplate> <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("audit") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("effective_time") %>' Width="50" ></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="审批"> <EditItemTemplate> <asp:TextBox ID="TextBox10" runat="server" Text=''></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Button runat="server" ID="orderManage" Text="通过" CommandName="Modify" CommandArgument='<%# Bind("id") %>' /> <asp:Button runat="server" ID="Button3" Text="不通过" CommandName="Forbid" CommandArgument='<%# Bind("id") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="审批备注" SortExpression="audit"> </Columns> </asp:GridView>
要在 GridView1_RowCommand 方法中取TextBox9的值,无法直接取。
这个时候要用下面的办法
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { string workid = e.CommandArgument.ToString(); string name = e.CommandName.ToString(); string strUpdate = ""; string strRemark = ""; string strEffective = ""; GridViewRow row = ((Control)e.CommandSource).BindingContainer as GridViewRow; //取有效时间 TextBox txtEffectiveTime = (TextBox)row.FindControl("TextBox9"); strEffective = (string.IsNullOrEmpty(txtEffectiveTime.Text) != true) ? txtEffectiveTime.Text.Trim() : ""; //取审备注 TextBox txtNotion = (TextBox)row.FindControl("TextBox12"); strRemark = (string.IsNullOrEmpty(txtNotion.Text) != true) ? txtNotion.Text.Trim() : ""; }
最有效的就是这段
GridViewRow row = ((Control)e.CommandSource).BindingContainer as GridViewRow;
//如果要去id为TextBox9的值
TextBox txtEffectiveTime = (TextBox)row.FindControl("TextBox9");
参考文章:http://www.cnblogs.com/cnaspnet/archive/2007/03/09/669410.html