制作Buttonfield执行javascript

mR.*_*iOt 3 c# asp.net c#-4.0

我想在gridview的ButtonField的lcick上执行JavaScript函数

由于没有onclick或onclient点击存在

  <asp:ButtonField HeaderText="Submit" Text="Submit" CommandName="Submit"  />
Run Code Online (Sandbox Code Playgroud)

ada*_*ost 5

处理RowDataBound活动.

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');");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)