N
Nomen Nescio
Hi
I've been trying to add a control dynamically to my page in order to pass a value to some client side script.
I've solved that problem with a different approach now but I'd still like to know what was going on with my
first method. On first load the following example will add MyTextBox to the placeholder and some JavaScript
which knows the field ID to look for can read its content. So far so good. However, after a PostBack I'd like
to pass a new value back to the client but creating a new textbox with the same ID renders an input field with
the original value not the new one, and attempting to find the original control to modify it that way doesn't
find anything either.
Any ideas anyone? Any help would be much appreciated.
Cheers, John.
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
Control c = PlaceHolder1.FindControl("MyTextBox");
if (c!=null)
{
(c as TextBox).Text = "MyValue" + DateTime.Now.ToString("hhmmss");
}
else
{
AddNewTextBox();
}
}
else
{
AddNewTextBox();
}
}
private void AddNewTextBox()
{
System.Web.UI.WebControls.TextBox MyTextBox = new TextBox();
MyTextBox.ID="MyTextBox";
MyTextBox.Text = "MyValue" + DateTime.Now.ToString("hhmmss");
PlaceHolder1.Controls.Add(MyTextBox);
}
I've been trying to add a control dynamically to my page in order to pass a value to some client side script.
I've solved that problem with a different approach now but I'd still like to know what was going on with my
first method. On first load the following example will add MyTextBox to the placeholder and some JavaScript
which knows the field ID to look for can read its content. So far so good. However, after a PostBack I'd like
to pass a new value back to the client but creating a new textbox with the same ID renders an input field with
the original value not the new one, and attempting to find the original control to modify it that way doesn't
find anything either.
Any ideas anyone? Any help would be much appreciated.
Cheers, John.
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
Control c = PlaceHolder1.FindControl("MyTextBox");
if (c!=null)
{
(c as TextBox).Text = "MyValue" + DateTime.Now.ToString("hhmmss");
}
else
{
AddNewTextBox();
}
}
else
{
AddNewTextBox();
}
}
private void AddNewTextBox()
{
System.Web.UI.WebControls.TextBox MyTextBox = new TextBox();
MyTextBox.ID="MyTextBox";
MyTextBox.Text = "MyValue" + DateTime.Now.ToString("hhmmss");
PlaceHolder1.Controls.Add(MyTextBox);
}