Modifying a programmatically added field after PostBack

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);
}
 
H

Hugo Flores

The bad news is that you'll never find the textbox control in your
place holder. Why? Because you have to remember that each time you
postback everything is lost, therefore, your peviously added textbox is
gone, and of course, not found by your place holder.

The worse news is that even if you always add the textbox, on each
postback, for some reason it won't change the text in your textbox (I
have already tested that). Why? I have no idea.

Unfortunately, there are no good news. But I have a question. Why do
you want to dinamically generate a textbox, that seems to me by your
code that will always be there, and the only thing that changes is the
text inside of it , and some JavaScript that I'm not sure if it's part
of the textbox or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top