制作Buttonfield执行javascript

时间:2012-01-25 11:23:51

标签: c# asp.net c#-4.0

我想在点击gridview的ButtonField

时执行JavaScript函数

由于没有onclick vs onclientclick present

  <asp:ButtonField HeaderText="Submit" Text="Submit" CommandName="Submit"  />

4 个答案:

答案 0 :(得分: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');");
            }
        }
    }

答案 1 :(得分:2)

对于此方案,您无法使用ButtonField,请更好地检查Steve回答的Javascript before asp:ButtonField click

快乐编码!!

答案 2 :(得分:1)

尝试以下代码

protected void GridView1_RowDataBound(object sender, GridViewRowCommandEventArgs e)
{

 if(e.Row.RowIndex != -1)
 {
  //Here Index of Cell will be the No of Delete Column.

  LinkButton btn = (LinkButton)e.Row.Cells[1].Controls[1];
  btn.Attributes.Add("onclick","if (confirm('Are you sure to delete this admin user?')){return true} else {return false}");

}
}

答案 3 :(得分:0)

在grdRewards_RowDataBound中我们可以写

  If e.Row.RowType = DataControlRowType.DataRow Then
       Dim btnbutton As LinkButton = DirectCast(e.Row.Cells(13).Controls(0), LinkButton)
       btnbutton.Attributes.Add("onclick", "javascript:return ShowDialog()")