0

我正在使用中的OnClick事件EditButtonGridView

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 循环,但这也不起作用。

4

2 回答 2

0

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

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

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

于 2012-04-18T11:05:23.397 回答
0

使用模板字段添加按钮

<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 中获得行索引

于 2012-04-17T11:30:04.310 回答