【问题标题】:get CommandArgument from GridViewRow asp webform从 GridViewRow asp webform 获取 CommandArgument
【发布时间】:2018-03-25 10:06:26
【问题描述】:

有没有可能从GridViewRow得到CommandArgument

在按钮点击事件的 for-each 循环中我需要CommandArgument

示例代码:

foreach (GridViewRow row in MyGridView.Rows)
   {
       ...
       string commandArgument = ?;
       ...
   }

【问题讨论】:

  • 你能解释一下你为什么需要那个吗?
  • @MAdeelKhalid 每行都有复选框。对于每个复选框,如果它被选中,则更新绑定到行的数据
  • CommandArgument 不是这样工作的。你可能需要DataKeyNames。但是由于缺乏信息,很难说。

标签: c# asp.net gridview webforms


【解决方案1】:

根据您的 cmets,我了解您的任务。你可以解决这个 通过创建隐藏字段来获取表的 ID,您可以在选中复选框时访问此 ID。然后根据id进行更新。我在这里更新 网格视图的名称列。

示例: WebForm1.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">
            <Columns>
                <asp:TemplateField HeaderText="name">
                    <ItemTemplate>
                        <asp:Label ID="lblUpdate" runat="server" Text= '<%#Eval("name") %>'></asp:Label> 
                        <asp:CheckBox ID="chkbox" runat="server" />
                         <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

WebForm1.aspx.cs

protected void btnSave_Click(object sender, EventArgs e)        {

        foreach (GridViewRow row in GridView1.Rows)
        {

            if (row.RowType==DataControlRowType.DataRow)
            {
                CheckBox chkbox = (CheckBox)row.Cells[0].FindControl("chkbox");
                HiddenField hdID = (HiddenField)row.Cells[0].FindControl("id");
                Label lbl = (Label)row.Cells[0].FindControl("lblUpdate");
                if (chkbox!=null)
                {
                    if(chkbox.Checked)
                    {
                        string Id = hdID.Value;
                        //now update name...  here by the help of this RowId===> Id
                    }
                }
            }

        }

注意这里的 Cells[0] 表示第一个 TemplateField 数据。我之所以使用它,是因为我已将所有名称字段和复选框字段放在第一个模板字段中。

我认为这会对您有所帮助。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 2021-07-02
    • 2012-03-07
    相关资源
    最近更新 更多