简体   繁体   English

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

[英]Execute Javascript function from GridView ItemTemplate button click ASP.NET Web Forms

I have a DataGridView that has two ItemTemplate button columns that are dynamically generated. 我有一个DataGridView,它具有两个动态生成的ItemTemplate按钮列。 I have code-behind for the both of the buttons that fire when they are clicked, but I also need a javascript function to run when the btnInfo button is clicked 对于单击时会触发的两个按钮,我都有代码隐藏,但是单击btnInfo按钮时,我还需要一个JavaScript函数才能运行

markup: 标记:

 <asp:GridView ID="gridResutls" runat="server" OnRowCommand="gridResutls_RowCommand" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="strName" HeaderText="Name"/>
            <asp:BoundField DataField="strBrand" HeaderText="Brand"/>
            <asp:BoundField DataField="strServing" HeaderText="Serving"/>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
                            Text="More Info" CommandArgument='<%# Eval("strID") %>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
                            Text="Add To Log" CommandArgument='<%# Eval("strID") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
        </Columns>
    </asp:GridView>'

I have a javascript function called populateLabel() that I need to fire off when btnInfo is clicked. 我有一个名为populateLabel()的javascript函数,单击btnInfo时需要将其触发。

Any ideas? 有任何想法吗? (I know similar questions have been asked and I looked throughout the posts and tried a few things but nothing seems to work) (我知道有人问过类似的问题,我浏览了所有帖子并尝试了一些方法,但似乎无济于事)

EDIT: Here is what the code-behind for the ItemTemplate buttons looks like 编辑:这是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
        }
    }

You can do something like this. 你可以做这样的事情。

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

markup: 标记:

<script type="text/javascript">
    function populateLabel() {
        alert('hi');
    }

</script>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="DSModels" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
                    Text="More Info" CommandArgument='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
                    Text="Add To Log" CommandArgument='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Use OnClientClick : 使用OnClientClick

<asp:Button ID="btnInfo" runat="server" OnClientClick="populateLabel()" CausesValidation="false" CommandName="MoreInfo"
  Text="More Info" CommandArgument='<%# Eval("strID") %>'/>

Example here. 这里的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM