简体   繁体   English

网格视图选定的索引

[英]Grid View selected index

I have a grid which contains a edit button . 我有一个包含编辑按钮的网格。 When i click the edit button and debug it does not hit to the selected index change event . 当我单击编辑按钮并进行调试时,它没有点击选定的索引更改事件。 There are no build errors 没有构建错误

code behind the grid 网格背后的代码

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>

The GridView 's SelectedIndexChanged event is tied to the RowCommand event. GridViewSelectedIndexChanged事件与RowCommand事件相关联。

The simple way to get the SelectedIndexChanged event to fire is to use the AutoGenerateSelectButton property of the GridView , like this: 获取SelectedIndexChanged事件的简单方法是使用GridViewAutoGenerateSelectButton属性,如下所示:

<asp:GridView AutoGenerateSelectButton="true"

This will add a button to each row with the text Select and when clicked, the SelectedIndexChange event will fire. 这将为每行添加一个按钮,文本为Select ,单击时,将触发SelectedIndexChange事件。


In the case of your edit button, you can use the CommandField in the grid view markup, like this: 对于编辑按钮,可以在网格视图标记中使用CommandField ,如下所示:

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

Now in your code-behind, you can handle the RowCommand event, like this: 现在在您的代码隐藏中,您可以处理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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM