0

I have a GridView in which I have put an extra column Details, and in each row of the GridView, I have a LinkButton called Details. So when I click this I want an event to trigger.

The asp.net code is:

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

ユーザーがリンクをクリックしGridViewRowた場所のが必要な場合(たとえば、 otherでコントロールを見つける場合)、 プロパティを使用できます。GridViewtemplateFieldsNamingContainer

于 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 に答える