Reputation: 1213
I have a button within a gridview and when I click the button I need to get the text value of that button (which is different for each row).
Is there a gridview event that I can use so I can get the row of the button that was clicked in the code behind?
<asp:GridView ID="gvResults" runat="server" Width="100%" AllowSorting="True" OnSorting="gvResults_Sorting" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Customer Type">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustType") %>' CommandName="Delete" />
<%-- <a href="javascript:sendval('<%# DataBinder.Eval(Container.DataItem, "CustType") %>');"><%# DataBinder.Eval(Container.DataItem, "CustType") %></a>--%>
</ItemTemplate>
</asp:TemplateField>
Upvotes: 0
Views: 7700
Reputation: 1874
Where possible, don't use DataBinder.Eval as such expressions are late-bound using reflection, and will negatively affect performance: -
http://www.devcurry.com/2011/02/how-to-avoid-databindereval-in-aspnet.html
ASP .NET - What's going on behind an Eval()?
Upvotes: 0
Reputation: 133403
You can use RowCommand event of the GridView control.
protected void gvResults_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = gvResults.Rows[index];
// Add your code here
}
}
You can set CommandArgument
like
<asp:Button ID="Button1"
runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "CustType") %>'
CommandName="Delete"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
Bind Event like
<asp:GridView ID="gvResults" OnRowCommand="gvResults_RowCommand"
Upvotes: 4
Reputation: 14614
If your purpose is just getting the Text
value of the clicked button, first add the click event handler so Button1_Click
method will be executed when the button is clicked:
<asp:Button ID="Button1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustType") %>' OnClick="Button1_Click" />
then get the Text
value in Button1_Click
method like this:
protected void Button1_Click(object sender, EventArgs e)
{
// get the clicked button text value
string buttonText = ((Button)sender).Text;
}
Upvotes: 0
Reputation: 84
Code is below:
In .aspx
<asp:GridView ID="GridView1" Width="100%" AutoGenerateSelectButton="false" runat="server" AllowPaging="true" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Customer Type">
<ItemTemplate>
<asp:Button runat="server" ID="button1" Text='<%#Eval("TenLop") %>' OnClick="Button1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In code behide
protected void Button1_Click(object sender, EventArgs e)
{
Button button = sender as Button;
TextBox1.Text = button.Text;
}
Hope this help.
Upvotes: 0