Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Buttonfield Execute a javascript

Tags:

c#

asp.net

c#-4.0

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"  />
like image 783
mR.idiOt Avatar asked Nov 26 '25 19:11

mR.idiOt


2 Answers

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');");
            }
        }
    }
like image 117
KV Prajapati Avatar answered Nov 29 '25 08:11

KV Prajapati


You cannot work with ButtonField for this scenario, better check Javascript before asp:ButtonField click answered by Steve.

Happy Coding !!

like image 24
Ravi Vanapalli Avatar answered Nov 29 '25 07:11

Ravi Vanapalli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!