views:

137

answers:

2

Hi All, I have UserControl and I need to add (generate) some tags, basically Input tags. Later on on postback I need collect values from these inputs. I use Render method to generate Inputs, but I dont know how can I get the values from these inputs on Postback. I do have unique id for each Input.

Code form Render method:

writer.Write(string.Format("<p>{0}</p>", Resources.CustomControls.inpCodeRestriction));
writer.RenderBeginTag(HtmlTextWriterTag.P);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write(product.Name);
writer.RenderEndTag();
TextBox tb = new TextBox();
tb.ID = string.Format("code{0}{1}", item.Id, item.ProductId);
tb.Text = string.Empty;
tb.ToolTip = Resources.CustomControls.titCodeRestriction;
tb.RenderControl(writer);
writer.RenderEndTag();

How can I get the value of Input on Postback. I tried Page.FindControl(), but it does not work for me.

Thanks for any advice.

A: 

You don't. If you use WebForms, you should create your child controls in the CreateChildControls method.

erikkallen
I used CreateChildControls as you adviced. I got thinks done. Thanks. Anyway I am not sure if the approach I took originaly is worst or better. I guess it may work if I get the value from the Request.Form collection as described below. Also CreateChildControls is processed twice which requires some extra work.
Xabatcha
If you don't use the CreateChildControls approach, you probably shouldn't use WebForms. Take a look at MVC!
erikkallen
A: 

Page.FindControl doesn't work because the controls are being rendered explicitly (i.e., they're not being added to the Page.Controls collection).

You can inspect the Request.Forms collection for keys that are named id_value, where id is the name of your control. This collection contains the posted data from WebForms.

David Andres