0

how to assign values to text box in start pause button method which was dynamically created in c sharp

 public void btn_addtimer_Click(object sender, EventArgs e)
    {
        var panel1 = new Panel() 
            { 
                Size = new Size(500, 180), 
                Location = new Point(10, i), 
                BorderStyle = BorderStyle.FixedSingle 
            };
        TextBox textseconds = new TextBox();
        textseconds.Name = "txtseconds";
        textseconds.Location = new Point(350, 50);
        textseconds.KeyPress += textseconds_KeyPress;
        panel1.Controls.Add(textseconds);


        Button startpause = new Button();
        startpause.Name = "btnstartpause";
        startpause.Text = "Start";
        startpause.Location = new Point(350, 80);
        startpause.Click += btnstartpause_Click;
        panel1.Controls.Add(startpause);

    }
  • 1
    You are assigning values to several properties of the `Button` already in this code. What do you mean? – Thorarin Dec 26 '13 at 14:35
  • 1
    Why dynamically create the panel and button? You can add the panel and the button and hide the button: startpause.Visible =false; then show it when needed. You can assign whatever values you need at that time. – JFV Dec 26 '13 at 14:41
  • @user3132774 Can you post your `btnstartpause ` method ? – Suraj Singh Dec 26 '13 at 14:55
  • @user3132774 Check the answer you need to assign `id` to your textbox\ – Suraj Singh Dec 26 '13 at 15:09

2 Answers2

3

Use FindControl with a cast:

((TextBox)FindControl("textseconds")).Text = "Some text here";
rkawano
  • 2,443
  • 22
  • 22
0

Assign Id to them

        textseconds.Id ="txtbxScnds";

THEN USE

((TextBox)FindControl("textseconds")).Text = "Some text here";

FindControl method Searches the current naming container for a server control with the specified id parameter.

However you need to recreate your dynamic controls with every postback.

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36