1

我使用下面的代码从后面的代码中将按钮添加到 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) { .....

4

2 回答 2

0

当您手动添加控件和事件时,您必须在每次回发时添加它们。仅对网格进行一次数据绑定(if(!IsPostBack)),但在每次回发时执行此循环

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
于 2013-01-03T18:13:05.240 回答
0

我相信我在更新面板中的链接按钮有类似的问题。

我相信我将此触发器添加到包含网格视图控件的更新面板中。

<Triggers>
     <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
</Triggers>

解决问题。确保您的 gridview 已初始化onRowCommand="GridView_RowCommand"属性集,并且您的链接按钮具有CommandName=""属性集。

然后,当您的链接按钮事件触发时,您可以确定在GridView_RowCommand.

protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e) 
{
   if (e.CommandName == "Link Button Command Name")
   {
      // link button
   }
}

如果这仍然不能解决您的问题,您可能必须尝试在GridView_RowCreatedPre_Render 或页面中创建链接按钮。

于 2013-01-03T17:11:11.353 回答