1

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

标记:

 <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>'

我有一个调用populateLabel()btnInfo 时需要触发的 javascript 函数。

有任何想法吗?(我知道有人问过类似的问题,我查看了所有帖子并尝试了一些东西,但似乎没有任何效果)

编辑:这是 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
        }
    }
4

2 回答 2

4

你可以做这样的事情。

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

标记:

<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>
于 2013-11-08T21:20:14.137 回答
1

使用OnClientClick

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

这里的例子。

于 2013-11-08T21:23:34.713 回答