从网格视图中选择一行 asp.net



我正在尝试使用选择按钮链接从网格视图中选择一行,但是当我单击该按钮时,它不会调用该 C# 方法。所以,我想知道我可能做错了什么。请帮帮我。谢谢

形式.aspx-

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
    OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added." Height="72px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="723px">
    <Columns>
        <asp:TemplateField HeaderText="Title" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:Label ID="lbltitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttitle" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemStyle Width="150px"></ItemStyle>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Subtitle" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:Label ID="lblsubtitle" runat="server" Text='<%# Eval("Subtitle") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtsubtitle" runat="server" Text='<%# Eval("Subtitle") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemStyle Width="150px"></ItemStyle>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Content" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:Label ID="lblContent" runat="server" Text='<%# Eval("Content") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtContent" runat="server" Text='<%# Eval("Content") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemStyle Width="150px"></ItemStyle>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" Text="SELECT" CommandName="MyCustomCommand" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

新闻提要演示.cs

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("MyCustomCommand"))
    {
        GridViewRow clickedRow = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
        Label lblID = (Label)clickedRow.FindControl("lblID");
    }
}

请使用此OnRowCommand="GridView1_RowCommand"

    <asp:LinkButton ID="lnkbedit" runat="server" CommandName="MyCustomCommand" CommandArgument='<%#Eval("id") %>'>Edit</asp:LinkButton>
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName == "Edit")
            {
            }
    }

使用方法GridView1_RowCommand,但它不绑定到GridView1 。您需要将OnRowCommand添加到网格视图中。

 <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">

最新更新