gpt4 book ai didi

asp.net - 当我从后面的代码将按钮添加到 GridView 的单元格时,单击事件不会调用

转载 作者:行者123 更新时间:2023-12-02 03:51:51 25 4
gpt4 key购买 nike

我使用下面的代码从代码后面将按钮添加到 GridView 的单元格,但是当我单击我添加的每个按钮时,单击事件 (lnk_Click) 不会调用,并且在我单击它后按钮会隐藏。我怎么能解决这个问题?

//aspx
<asp:GridView ID="GridView1" runat="server" GridLines="None" Width="940px"
OnSelectedIndexChanged="grid_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound" AutoGenerateColumns="False" CssClass="gridview">
<Columns>
<asp:TemplateField HeaderText="...">
<ItemTemplate>
<asp:Panel ID="pnlSteps" runat="server"></asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

//Code behind
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Panel pnl = (Panel)GridView1.Rows[i].Cells[0].FindControl("pnlSteps");
LinkButton lnk = new LinkButton();
lnk.Text = "...";
pnl.Controls.Add(lnk);
lnk.Click += new EventHandler(lnk_Click);
}//for
...

protected void lnk_Click(object sender, EventArgs e)
{
//...
}

//编辑

我用下面的代码填充gridview

//dsSet is DataSet that i fill it with data from Database
DataTable dtt = new DataTable();

dtt.Columns.Add(new DataColumn("status", typeof(string)));
dtt.Columns.Add(new DataColumn("finantial", typeof(string)));
dtt.Columns.Add(new DataColumn("phone", typeof(string)));
dtt.Columns.Add(new DataColumn("name", typeof(string)));
dtt.Columns.Add(new DataColumn("code", typeof(string)));

for (int i = 0; i < dsSet.Tables[0].Rows.Count; i++)
{
DataRow dr = dtt.NewRow();

dr[0]=...;
dr[1]=...;
dr[2]=...;
dr[3]=...;
dr[4]=...;

dtt.Rows.Add(dr);
}//for
GridView1.DataSource = dtt;
GridView1.DataBind();

最佳答案

因为按钮在 gridview 中,所以你不能在 onclick 中获取它的点击事件,但你必须使用 GridView.RowCommand并在按钮中使用 commandname

编辑

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="commandName"

Text="command" />
</ItemTemplate>
</asp:TemplateField>

代码隐藏:

protected void GridView1_RowCommand(object sender, 
GridViewCommandEventArgs e)
{
if (e.CommandName == "commandName")
{
// add your code
}

}

关于asp.net - 当我从后面的代码将按钮添加到 GridView 的单元格时,单击事件不会调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088544/

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