views:

25

answers:

1

I have a GridView which contains a linkbutton in its templatefield. What I want to know is, when I click on the linkbutton, how can I get the text of the linkbutton into a string called 'name'?

+3  A: 

Provide event handler for Click event:

<asp:GridView id="myGrid" runat="server">
<columns>
   <asp:templatefield>
      <itemtemplate>
         <asp:LinkButton id="MyButton" 
           Text="SuperText" 
           OnClick="MyButton_Click" 
           runat="server"/>
      </itemtemplate>
   </asp:templatefield>
</columns>
</asp:GridView>

In event handler use the following code:

protected void MyButton_Click(object sender, EventArgs e)
    {
       LinkButton btn = sender as LinkButton;
       if(btn != null)
          string name = btn.Text; // SuperText

    }
Pavel Morshenyuk
Thank you very much Pavel. This is what I am looking for. :)
pRAVeEN
Please mark answer as correct if it is what you looking for - so other people will be able to see it. :)
Pavel Morshenyuk