【问题标题】:OnClick Event of EditButton in GridViewGridView中EditButton的OnClick事件
【发布时间】:2012-05-29 13:30:03
【问题描述】:

我在GridView中使用EditButtonOnClick事件

protected void editclick(object sender, EventArgs e)
{
    try
    {
        Button EditButton = (Button)gvUserMaster.FindControl("edit_btn");
        tblAddEdit.Visible = true;
    }
    catch(Exception ex)
    {
    }
}

但我得到了例外:

找到了具有相同 ID 'lblUserName' 的多个控件。 FindControl 要求控件具有唯一的 ID。

我尝试使用 foreach 循环,但也没有用。

【问题讨论】:

  • 您能出示您的 ASPX 代码吗?错误消息表明一些lblUserName
  • 好的,lblUsername 中有冗余,我解决了这个问题,但是你能不能得到编辑按钮的行 ID。

标签: c# asp.net gridview


【解决方案1】:

我通过在编辑按钮单击事件上编写编辑函数来解决,并获取所选行的行ID。

int rindex = (((GridViewRow)(((Button)(sender)).Parent.BindingContainer))).RowIndex;
 Button EditButton = (Button)gvUserMaster.Rows[rindex].FindControl("btnEdit");

rindex 为您提供 rowindex,EditButton 为您提供所选行的 id。

【讨论】:

    【解决方案2】:

    使用模板字段添加按钮 喜欢

    <asp:TemplateField HeaderText="Edit">
        <ItemTemplate>
            <asp:LinkButton runat="server" ID="lnkEdit" Text="Edit"
                CausesValidation="false" CommandArgument='<%# Container.DataItemIndex %>'
                OnCommand="lnkEdit_Command"></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    

    在后面的代码中:

    protected void lnkEdit_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
    {
        id = e.CommandArgument.ToString();
        --add your code
    }
    

    您将在 id 中获得行索引

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多