本文介绍了动态按钮没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设计:



label1 textbox1 button1 点击button1然后



label1 textbox1 button1

label2 textbox2 button2 点击button2然后





label1 textbox1 button1

label2 textbox2 button2

label3 textbox3 button3 点击按钮3然后





添加 label4,textbox4,button4 上面的控制和















怎么做到这一点??

我试过但点击了动态生成按钮,控件消失了。

我用过:

aspx:





Design:

label1 textbox1 button1 click on button1 then

label1 textbox1 button1
label2 textbox2 button2 click on button2 then


label1 textbox1 button1
label2 textbox2 button2
label3 textbox3 button3 click on button3 then


adds label4,textbox4 , button4 to above controls and

.
.
.
.



How to achieve this??
I tried but on clicking the dynamically generated button, the controls are getting vanished.
I used:
aspx:


<table border="1" cellpadding="0" cellspacing="0" id="tblGenerate" runat="server">
        <tr>
          <td>
              <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
          </td>
          <td>
              <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
          </td>
          <td>
              <asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="btnAdd_Click"/>
          </td>
          <td>
              <asp:Label ID="lblCost" runat="server"></asp:Label>
          </td>
      </tr>
      <tr>
          <td  id="trGenerate" runat="server" colspan="3">
          </td>
      </tr>
  </table>





aspx.cs:



aspx.cs:

protected void Page_Load(object sender, EventArgs e)
        {
 
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            //Recreate Controls
            RecreateControls("lblName", "Label");
            RecreateControls("txtValue", "TextBox");
            RecreateControls("btnAddNew", "Button");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int cnt = FindOccurence("txtValue");

            CreateLabel("lblName_" + Convert.ToString(cnt + 1));
            CreateTextBox("txtValue_" + Convert.ToString(cnt + 1));
            CreateButton("btnAddNew_" + Convert.ToString(cnt + 1));



            //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert",

            //             "<script type = 'text/javascript'>alert('" + ID +

            //              " fired OnTextChanged event5');</script>");
        }

        private int FindOccurence(string substr)
        {

            string reqstr = Request.Form.ToString();

            return ((reqstr.Length - reqstr.Replace(substr, "").Length)

                              / substr.Length);

        }

        private void CreateTextBox(string ID)
        {
            TextBox txt = new TextBox();

            txt.ID = ID;

            trGenerate.Controls.Add(txt);

            Label lblInstance = new Label();

            lblInstance.Width = 10;

            lblInstance.Height = 20;

            trGenerate.Controls.Add(lblInstance);

        }

        private void CreateLabel(string ID)
        {
            Label lblInstance = new Label();

            lblInstance.ID = ID;

            lblInstance.Text = "Name";

            lblInstance.Width = 50;

            trGenerate.Controls.Add(lblInstance);
        }

        private void CreateButton(string ID)
        {
            Button btnAddtxt = new Button();

            btnAddtxt.ID = ID;

            btnAddtxt.Text = "Add New";

            btnAddtxt.Click += new System.EventHandler(btnAdd_Click);

            btnAddtxt.CausesValidation = true;

            trGenerate.Controls.Add(btnAddtxt);

            Literal lt = new Literal();

            lt.Text = "<br />";

            trGenerate.Controls.Add(lt);
        }

         

        private void RecreateControls(string ctrlPrefix, string ctrlType)
        {

            string[] ctrls = Request.Form.ToString().Split('&');

            int cnt = FindOccurence(ctrlPrefix);

            if (cnt > 0)
            {
                for (int k = 1; k <= cnt; k++)
                {

                    for (int i = 0; i < ctrls.Length; i++)
                    {

                        if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString())

                            && !ctrls[i].Contains("EVENTTARGET"))
                        {

                            string ctrlID = ctrls[i].Split('=')[0];



                            if (ctrlType == "TextBox")
                            {

                                CreateTextBox(ctrlID);

                            }



                            if (ctrlType == "Label")
                            {

                                CreateLabel(ctrlID);

                            }


                            if (ctrlType == "Button")
                            {
                                CreateButton(ctrlID);
                            }
                        }

                    }

                }

            }

        }
 
    }

推荐答案


这篇关于动态按钮没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 07:12