我使用下面的代码从后面的代码中将按钮添加到 GridView 的单元格when user click on a button
,但是当我单击我添加的每个按钮时,单击事件(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();
//我也使用CommandName
如下命令并设置GridView_RowCommand
为 GridviewOnRowCommand
但不起作用
lnk.CommandName = "选择";
protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e) { .....