简体   繁体   中英

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.

The simple way to get the SelectedIndexChanged event to fire is to use the AutoGenerateSelectButton property of the GridView , like this:

<asp:GridView AutoGenerateSelectButton="true"

This will add a button to each row with the text Select and when clicked, the SelectedIndexChange event will fire.


In the case of your edit button, you can use the CommandField in the grid view markup, like this:

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

Now in your code-behind, you can handle the RowCommand event, like this:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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