【问题标题】:Make a Buttonfield Execute a javascript制作一个 Buttonfield 执行一个 javascript
【发布时间】:2012-01-25 11:23:50
【问题描述】:

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

因为没有 onclick 或 onclient click 存在

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

【问题讨论】:

  • 我想你需要在Row DataBound事件中设置这个
  • @V4Vendetta 如何在 RowDataBound 中找到这个 ButtonField?
  • 这几行e.Row.Cells[youwouldknowthisindex].Controls[0]

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


【解决方案1】:

处理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');");
            }
        }
    }

【讨论】:

    【解决方案2】:

    您不能在这种情况下使用 ButtonField,最好检查 Javascript before asp:ButtonField click Steve 回答的问题。

    编码愉快!!

    【讨论】:

    • 我已经检查过了。但是我在这里非常需要这个按钮域。
    【解决方案3】:

    试试下面的代码

    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}");
    
    }
    }
    

    【讨论】:

      【解决方案4】:

      在 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()")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        • 2013-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多