0

我有一个包含编辑按钮的网格。当我单击编辑按钮并进行调试时,它不会命中选定的索引更改事件。没有构建错误

网格后面的代码

public void btnModemDetailsEdit_Click(object sender, EventArgs e)
{
    isEdit = true;
}

protected void gridModemDetails_SelectedIndexChanged(object sender, EventArgs e)
{
    int id = Convert.ToInt32(GridModemDetails.DataKeys[GridModemDetails.SelectedIndex].Values["gridModemDetails_SelectedIndexChanged"].ToString());
}

<asp:GridView ID="GridModemDetails" runat="server" Width="435px" 
              DataKeyNames="ModemId" AllowPaging="True"
              OnSelectedIndexChanged="gridModemDetails_SelectedIndexChanged"
              AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Edit" Visible="True" >
            <ItemTemplate>
                <asp:LinkButton ID="btnModemDetailsEdit" 
                                AccessibleHeaderText="Edit" 
                                ButtonType="Button" 
                                Text="Edit" 
                                HeaderText="Edit" 
                                runat="server" 
                                OnClick="btnModemDetailsEdit_Click"/>
            </ItemTemplate>
        </asp:TemplateField>
4

1 回答 1

3

的事件与GridView事件相关联。SelectedIndexChangedRowCommand

SelectedIndexChanged事件触发的简单方法是使用 的AutoGenerateSelectButton属性GridView,如下所示:

<asp:GridView AutoGenerateSelectButton="true"

这将为带有文本的每一行添加一个按钮,Select单击时将触发该SelectedIndexChange事件。


对于您的编辑按钮,您可以CommandField在网格视图标记中使用,如下所示:

<asp:GridView ...>
    <Columns>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>

现在在您的代码隐藏中,您可以处理该RowCommand事件,如下所示:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit") 
    {
        // Get the actual row
        GridViewRow theGridViewRow = GridView1.Rows(e.RowIndex);

        // Do something with grid view row here
    }
}
于 2013-11-04T04:05:02.717 回答