Click here to Skip to main content
15,908,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if (dt1.Rows.Count > 0)
  for (int i = 0; i < dt1.Rows.Count; i++)
     Panel pnlTextBox= new Panel();
     pnlTextBox.ID = "pnlTextBox_" + i.ToString();
     pnlTextBox.CssClass = "tagpanel";
     TextBox txt = new TextBox();                //Textbox
     txt.ID = "txt_" + i.ToString();
     txt.CssClass = "txtb";
     txt.AutoPostBack = true;
     txt.EnableViewState = true;
     txt.Text = dt1.Rows[i][5].ToString();
     if (dt1.Rows[i][1].ToString() == "1")
        txt.Enabled = false;
     txt.MaxLength = 8;
     txt.AutoPostBack = true;

     Button Btn = new Button();                 //Save Button
     Btn.ID = "Btn_" + i.ToString();
     Btn.Text = "Save";
     Btn.CssClass = "btnCss";
     Btn.Click += new System.EventHandler(this.Btn_test);

     Literal ltbr1 = new Literal();              //new line 
     ltbr1.ID = "ltbr1" + i.ToString();
     ltbr1.Text = "<br />";

     Label lbl = new Label();                  //tag name
     lbl.ID = "lbl_" + i.ToString();
     lbl.CssClass = "lblCss";
     lbl.Text = dt1.Rows[i][0].ToString();

     Label TagId = new Label();              //tag id
     TagId.ID = "Tag_" + i.ToString();
     TagId.Text = dt1.Rows[0][6].ToString();
     TagId.Visible = false;

     Label OldValue = new Label();             //tag value...used to store old value
     OldValue.ID = "old_" + i.ToString();
     OldValue.Text = dt1.Rows[i][3].ToString();
     OldValue.Visible = false;

     //    pnlTextBox.Controls.Add(img);
     if (dt1.Rows[i][1].ToString() == "1")
         //    pn1.Controls.Add(ltbr);
         //  pn2.Controls.Add(ltbr); 

this is my Code to create dynamic control.I have call this function at page pre_init state. Button event is also worked properly. But i have one problem.When I am write new value in text box which is dynamically created causes page load. Why this is happennig??? Because of this when i am changing the value in textbox and if my focus is in textbox then click event of button not works.It will work after page load. why this is happening?? where should i went wrong??
Updated 2-Mar-15 20:58pm
Anisuzzaman Sumon 3-Mar-15 3:06am    
txt.AutoPostBack = true; change it as txt.AutoPostBack = false;
Yes, correct. :) High five.
Member 11151142 3-Mar-15 3:36am    
Thank you...sooo Much...

Yes Anisuzzaman Sumon is correct. It is happening due to the AutoPostBack Property.

Remove that. That is absolutely not needed.
Share this answer
you have enabled the auto postback for text box,
Make sure when ever you have adding dynamic controls, AutoPostback values should be false

TextBox txt = new TextBox(); //Textbox
txt.ID = "txt_" + i.ToString();
txt.CssClass = "txtb";
txt.AutoPostBack = false;
txt.EnableViewState = true;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900