从GridView ItemTemplate按钮执行Javascript函数单击ASP.NET Web窗体

我有一个DataGridView,它有两个动态生成的ItemTemplate按钮列。 我点击它们时触发的两个按钮都有代码隐藏,但是当点击btnInfo按钮时我还需要一个javascript函数来运行

标记:

       <asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo" Text="More Info" CommandArgument=''/>     <asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem" Text="Add To Log" CommandArgument='' />    ' 

我有一个名为populateLabel()的javascript函数,我需要在单击btnInfo时触发。

有任何想法吗? (我知道有类似的问题已被问过,我在整个post中查看并尝试了一些事情,但似乎没有任何工作)

编辑:这是ItemTemplate按钮的代码隐藏的样子

 protected void gridResutls_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) { // Kick out if neither of the two dynamically created buttons have been clicked if (e.CommandName != "MoreInfo" && e.CommandName != "AddItem") { return; } // If the "More Info" buton has been clicked if (e.CommandName == "MoreInfo") { // Some code } // If the "Add To Log" button has been clicked if (e.CommandName == "AddItem") { // Some code } } 

你可以做这样的事情。

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { Button btnAdd = e.Row.FindControl("btnAdd") as Button; btnAdd.Attributes.Add("OnClick", "populateLabel();"); } } 

标记:

                  

使用OnClientClick

  

这里的例子。