gpt4 book ai didi

c# - ASP.NET Gridview ButtonField onclick 触发包含行的 onclick 事件

转载 作者:行者123 更新时间:2023-11-28 01:39:13 25 4
gpt4 key购买 nike

我有一个 gridview 行,单击时必须执行回发 A,该行中有一个按钮字段,单击时必须执行回发 B。问题是,当我单击按钮字段时,event1 和 event2 都会被触发。下面是代码。

protected void gdv_RowCommand(object sender, GridViewCommandEventArgs e)
{
string arg = Convert.ToString(((System.Web.UI.WebControls.CommandEventArgs)(e)).CommandArgument);

if (e.CommandName == "Command1")
{
doEvent1(arg);
}
else if (e.CommandName == "Command2")
{
doEvent2(arg);
}
}

protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton b1 = (LinkButton)e.Row.Cells[0].Controls[0];
matchesButton.CommandArgument = arg1;

LinkButton rowLink = (LinkButton)e.Row.Cells[1].Controls[1];
rowLink.CommandArgument = arg2;

e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(rowLink, "");
}
}

这是 gridview 的 asp 代码

<Columns>
<asp:ButtonField DataTextField="f1" HeaderText="H1" CommandName="Command1" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btn1" runat="server" Text="" CommandName="Command2" />
</ItemTemplate>
</asp:TemplateField>
</Columns>

最佳答案

尝试使用这个

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Command2")
{
// Your Code here
}
}

要在 GridView 中查找控件,请使用此代码

LinkButton lnkbtn= (LinkButton)e.Row.FindControl("btn1");

关于c# - ASP.NET Gridview ButtonField onclick 触发包含行的 onclick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21131048/

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