从asp.net的gridview中选择一行?

时间:2017-02-08 15:51:21

标签: c# asp.net gridview

我试图通过使用选择按钮链接从gridview中选择一行,但是当我单击该按钮时,它不会调用该C#方法。所以,我想知道我能做错什么。请帮帮我。感谢

form.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>

NewsFeedDemo.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");
    }
}

2 个答案:

答案 0 :(得分:0)

请使用此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")
            {
            }
    }

答案 1 :(得分:0)

您使用方法GridView1_RowCommand,但它未绑定到GridView1。您需要将OnRowCommand添加到GridView。

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