pRAVeEN
pRAVeEN

Reputation: 41

Gridview and LinkButton

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'?

Upvotes: 0

Views: 240

Answers (1)

Pavel Morshenyuk
Pavel Morshenyuk

Reputation: 11471

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

    }

Upvotes: 6

Related Questions