I want to execute a JavaScript function on the lcick of a ButtonField of a gridview
As there is no onclick or onclient click present
<asp:ButtonField HeaderText="Submit" Text="Submit" CommandName="Submit" />
Handle the RowDataBound event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Presume that the buttonField is at 1st cell
LinkButton link = e.Row.Cells[0].Controls[0] as LinkButton;
if (link != null)
{
link.Attributes.Add("onclick", "alert('Hello');");
}
}
}
You cannot work with ButtonField for this scenario, better check Javascript before asp:ButtonField click answered by Steve.
Happy Coding !!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With