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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19868454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:05:19  来源:igfitidea点击:

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

c#javascriptasp.netgridview

提问by Stas Bukhtiyarov

I have a DataGridView that has two ItemTemplate button columns that are dynamically generated. 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

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

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.

我有一个 javascript 函数populateLabel(),我需要在单击 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
        }
    }

回答by tdk

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>

回答by Coder

Use OnClientClick:

使用OnClientClick

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

Example here.

例子在这里。