Help with making a control work . Please help :-(

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi everyone,

I'm having a problem with getting a control that I'm using to work.

I have a placeholder control sitting within a user control.

On certain occasions at runtime I create a Textbox control and add it to the
placeholder. This works well almost all the time.

Unfortunately there is one scenario when it doesnt work. The textbox doesnt
seem to be initialised whereas it normally is.

Does anyone know why it should work sometimes but not others?

I think its something to do with me needing to initialise the Textbox
control myself. Could someone show me how to do that and where to put the
code? I've tried just creating a new textbox, setting its Text and then
adding it to the pages control collection. This just ends up adding a new
textbox to the form :-(

If anyone could help with this I would be very greatful. I've heard lots of
people saying that you need to initialise your user controls manually but iv
never seen a decent example of where and how to do it.

Thanks

Simon
 
Ok, thanks for your reply.

If the textbox should be displayed, as determined on the first request to
the page - I make a control and add it to the placeholder:

txtUsername = new TextBox();
txtUsername.ID = "txtUsername";
txtUsername.BorderWidth = 1;
txtUsername.BorderStyle = BorderStyle.Solid;
phUsernamePlaceholder.Controls.Add(txtUsername);



Later, if the situation is a postback I have:

txtUsername = new TextBox();
txtUsername.ID = "txtUsername";
txtUsername.BorderStyle = BorderStyle.Solid;
txtUsername.BorderWidth = new Unit(1);
txtUsername.Text = Request.Form["createUserControl:txtUsername"];

This now seems to be working but I'm not 100% sure why. Is this normally
what you have to do with controls? Seems quite simple. You don't have to do
anything special - just create a new txt box and make sure it has the same
name as the one that should be on the page?

Thanks

Simon
 
Back
Top