Reputation: 41
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
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