gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:11:18 25 4
gpt4 key购买 nike

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

标记:

 <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() 的 javascript 函数,我需要在单击 btnInfo 时触发它。

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

编辑:这是 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();");
}
}

标记:

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

关于c# - 从 GridView ItemTemplate 按钮执行 Javascript 函数单击 ASP.NET Web 窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19868454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com