I am creating a web site, in that I have created a textbox in a placeholder dynamically. But the problem is I can not access the text of the textbox in another method.
I have one button for create textbox and another button for reading the text of the textbox.
Event handler method of the create control button.
Code:
protected void Button1_Click(object sender, EventArgs e)
{
TextBox myText = new TextBox();
myText.Text = "Hello world";
PlaceHolder1.Controls.Add(myText);
}
and Event handler method of the read text button
Code:
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(((TextBox)PlaceHolder1.FindControl("myText")).Text);
}
In this code when i am trying to access the textbox inside placeholder, i am getting null reference exception. That means
placeholder has no control in it.
When i debug it, i get PlaceHolder1.Controls.Count is 0.
Why the control collection is empty? Is viewstate is not working? How to solve the problem?
Please help...