yuezhonghu

悠然现南山...
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

动态添加表格和控件,并为控件注册事件

Posted on 2008-01-24 17:31  .狐狸血.  阅读(433)  评论(0编辑  收藏  举报
记忆中这个东西繁琐,但是好用;会出现提交后不触发程序的错误,以下为解决方法

前台:
 <table width="100%" border="0" cellpadding="0" cellspacing="0" id="D_T_A" runat="server">
                                                    <tr id="TR_A" runat="server">
                                                        <td width="90" height="21" align="center" background="images/btnbg.gif">
                                                            <asp:LinkButton ID="MiaoShuLinkButton" runat="server" OnClick="MiaoShuLinkButton_Click">描述</asp:LinkButton></td>
                                                        <td width="90" align="center" background="images/btnbg.gif">
                                                            <asp:LinkButton ID="JiShuLinkButton" runat="server" OnClick="JiShuLinkButton_Click">技术参数</asp:LinkButton></td>
                                                        <td width="90" align="center" background="images/btnbg.gif" id="TD_TongL" runat="server">
                                                            <asp:LinkButton ID="XiangGuanLinkButton" runat="server" OnClick="XiangGuanLinkButton_Click">同类产品</asp:LinkButton></td>                                                                                                                                                 
                                                    </tr>
                                                </table>

后台:
    protected void Page_Load(object sender, EventArgs e)  
    {
        All_ShuXing(); //不要放在 if(!IspostBack)中,否则提交后动态生成的事件不执行并且生成的属性消失

   }

 /// <summary>
    /// 生成所有自定义属性的列和LinkButton控件
    /// </summary>
    protected void All_ShuXing()
    {
        if (Request["id"] != null && Request.QueryString["id"].ToString().Trim().Length > 0)
        {
            P_Attribute temp_A = new P_Attribute();
            string P_id = Request.QueryString["id"].ToString();
            string strSQL="select * from Attribute where a_p_id="+P_id;

            if (temp_A.GetListDataTable(strSQL, new ArrayList()))
            {
                for (int i = 0; i < temp_A.ThisDT.Rows.Count; i++)
                {
                    HtmlTableCell TD_A = new HtmlTableCell();// width="90" height="21" align="center" background="images/btnbg.gif"
                    TD_A.Align = "Center";
                    TD_A.Width = "90";
                    TD_A.Height = "21";
                    TD_A.Attributes.Add("background", "images/btnbg.gif");
                    //TD_A.background = "images/btnbg.gif";
                    LinkButton LB = new LinkButton();
                    LB.ID = "LinkButton_" + i.ToString();
                    //LB.ID = "LB_A";     //若此处定义 则出现 ID不唯一的错误.
                    LB.Text=temp_A.ThisDT.Rows[i]["A_Name"].ToString().Trim();
                    LB.CommandName = temp_A.ThisDT.Rows[i]["A_ID"].ToString().Trim();
                    LB.CommandArgument = temp_A.ThisDT.Rows[i]["A_ID"].ToString().Trim();
                    //LB.Click += new EventHandler(this.LB_Command);
                    LB.Command += new CommandEventHandler(this.LB_Command);

                   
                    TD_A.Controls.Add(LB);
                    TR_A.Cells.Add(TD_A);
                    //this.Page.Controls.Add(LB);
                }
                HtmlTableCell TD_A_1 = new HtmlTableCell();
                TD_A_1.InnerHtml = "&nbsp;";
                TR_A.Cells.Add(TD_A_1);
               
            }
            else
            {
                HtmlTableCell TD_A = new HtmlTableCell();
                TD_A.InnerHtml = "&nbsp;";
                TR_A.Cells.Add(TD_A);
            }

        }
    }
    /// <summary>
    /// 循环生成的LinkButton的绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    //protected void LB_Command(Object sender, EventArgs e)
    protected void LB_Command(Object sender,CommandEventArgs e)
    {
        P_Attribute temp_P = new P_Attribute();
        int ID = int.Parse(e.CommandName);
        if (temp_P.Getthis(ID))
        {
            ContentTable.InnerHtml = temp_P.A_Content;
        }
        //All_ShuXing();
    }


控件以下三个属性的原理弄清楚:
CommandName:命令名 可以传递

CommandArgument:参数 可以传递
Command:注册到事件