0

我有GridView一个额外的列Details,并且在 的每一行中GridView,我都有一个LinkButton称为 Details 的列。因此,当我单击此按钮时,我希望触发一个事件。

asp.net 代码为:

<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" Text="Details"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

请帮忙。谢谢你。

4

3 回答 3

3

然后使用LinkButton.Click-event

<asp:LinkButton ID="Details" OnClick="LinkClicked" runat="server" Text="Details">

在代码隐藏中:

protected void LinkClicked(Object sender, EventArgs e) 
{
    LinkButton link = (LinkButton)sender;
    GridViewRow row = (GridViewRow)link.NamingContainer; 
    // assuming there's a label with ID=Label1 in another TemplateField:
    Label label1 = (Label)row.FindControl("Label1");
    label1.Text="You clicked the link button";
}

如果您需要用户单击链接GridViewRowGridView位置,例如在 other 中查找控件templateFields,您可以使用该NamingContainer属性。

于 2012-09-26T12:03:54.573 回答
2

处理GridView.RowCommand 事件

在 GridView 控件中单击按钮时发生。

<asp:LinkButton ID="Details"  commandname="Details" runat="server" Text="Details"></asp:LinkButton>


void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Details")
    {
     // Convert the row index stored in the CommandArgument
     // property to an Integer.
     int index = Convert.ToInt32(e.CommandArgument);

    //Your Code

    }

}
于 2012-09-26T12:04:03.030 回答
1

您可以使用命令参数来确定单击了哪一行(假设您在一行中有多个按钮(标准按钮/链接按钮)

<asp:GridView OnRowCommand="GridViewOnItemCommand" runat="server">
  <asp:TemplateField HeaderText="Details">
    <ItemTemplate>
      <asp:LinkButton ID="btnDetails" CommandName="Details" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Details" runat="server"/>
      <asp:LinkButton ID="btnDelete" CommandName="Delete" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Delete" runat="server"/>
    <ItemTemplate>
  </asp:TemplateField>
</asp:GridView>

在文件后面的代码中

protected void GridViewOnItemCommand(object sender, GridViewCommandEventArgs e)
{
     //you can determine which button was clicked (detail or delete)
     var command = e.CommandName;

     //you can determine which row was clicked
     var arg = e.CommandArgument;

     if(command == "Details")
          ShowDetails(arg);
     if(command == "Delete")
          Delete(arg);
}

希望这可以帮助

于 2012-09-26T12:17:44.013 回答