GridView中的RowCommand事件中的取值问题GridView的RowCommand事件中获取该行DataKey值(转)

需求现象:在RowCommand事件中获取该行DataKey值,以便编辑改行相应的数据;

解决方案:
1)模板列中的LinkButton ,需要绑定其CommandArgument=’<%# Bind(“ID”) %>’,然后在RowCommand事件中获取
绑定:
<asp:TemplateField >

<asp:LinkButton ID=“deleteSelect” Text=“删除选中” CommandName=“DeleteSelect” runat=“server”></asp:LinkButton>


<asp:LinkButton ID=“delete” Text=“删除” CommandName=“DeleteRow” CommandArgument=’<%# Bind(“ID”) %>’ runat=“server” ></asp:LinkButton>

</asp:TemplateField>
获取:
string ID = e.CommandArgument.ToString();

2)非模板列的ButtonField,不需要绑定其CommandArgument,就可以获取
绑定:
<asp:ButtonField CommandName=“showprodorder” HeaderText=“查看” Text=“删除” >

</asp:ButtonField>
获取:
int rowNumber = Convert.ToInt32(e.CommandArgument);
string ID = dg.DataKeys[rowNumber].Value.ToString();
先看前台代码

<asp:GridView runat=“server” ID=“GridView1” CssClass=“Result_tab” AutoGenerateColumns=“False” DataSourceID=“SqlDataSource1” DataKeyNames=“id” OnRowCommand=“GridView1_RowCommand”>

<asp:TemplateField HeaderText=“有效时间” SortExpression=“audit”>

<asp:TextBox ID=“TextBox8” runat=“server” Text=’<%# Bind(“audit”) %>’></asp:TextBox>


<asp:TextBox ID=“TextBox9” runat=“server” Text=’<%# Bind(“effective_time”) %>’ Width=“50” ></asp:TextBox>

</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">
  要在 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”);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值