2

I am generating dynamic textbox controls on drop down selected index change event.

  protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (Attributes attribute in getAllAttributes(Convert.ToInt32(ddlCategories.SelectedValue)))
        {
            Panel div = new Panel();

            div.Attributes.Add("class", "form-group");
            HtmlGenericControl lbl = new HtmlGenericControl("label");

            lbl.Attributes.Add("class", "col-lg-2 control-label");
            lbl.InnerText = attribute.Name;

            Panel innerdiv = new Panel();
            innerdiv.Attributes.Add("class", "col-lg-10");

            TextBox txt = new TextBox();
            txt.ID = attribute.ID.ToString();
            txt.Attributes.Add("class", "form-control");
            innerdiv.Controls.Add(txt);

            div.Controls.Add(lbl);
            div.Controls.Add(innerdiv);
            CustomAttributes.Controls.Add(div);

        }
    }

Now after the user fill up the values in the form i want to get the values of the dynamically generated controls. But CustomAttributes.findControls("") doesn't work for me. it gives null all the time.

I also tried

var textBoxesInContainer = CustomAttributes.Controls.OfType<TextBox>();

but it also doesnt work.

Can any one please tell me what is going wrong here.

Thanks

Sachin Trivedi
  • 2,033
  • 4
  • 28
  • 57
  • CustomAttributes.Controls.FindControl() is not there ? – Sajith A.K. May 27 '14 at 07:23
  • Why doesn't `FindControl` work for you? – Patrick Hofman May 27 '14 at 07:24
  • You need a recursive search, because your TextBox is a child of `innerdiv` not `CustomAttributes` directly. http://stackoverflow.com/questions/253937/recursive-control-search-with-linq or http://stackoverflow.com/questions/4955769/better-way-to-find-control-in-asp-net. As mentioned, FindControl should work as you've given your TextBox an ID too – CodingIntrigue May 27 '14 at 07:24
  • @SajithA.K. it is there and i tried it. it returns me the null – Sachin Trivedi May 27 '14 at 07:24
  • in which event you have tried that? – Sajith A.K. May 27 '14 at 07:26
  • @SajithA.K. on button click event CustomAttributes.findconrols("id"). but it didn't work. – Sachin Trivedi May 27 '14 at 07:30
  • The controls created while selecting dropdown, so when page postback time it will not get.You have to create the control when postback. and the value you can access in the Page PreRender event. – Sajith A.K. May 27 '14 at 08:55
  • @SajithA.K. can you provide any code snippet ? on drop down change event the controls are getting generated. and on button click due to post back it loses those controls because asp.net doesn't maintain viewstate. So how can i deal with this problem ? – Sachin Trivedi May 27 '14 at 09:05
  • sachin I dont have visual studio to check your code. You do one thing, make a function using whatever you give inside the dropdown changes. And call that method in dropdown changes and page load(only when IsPostback). And findcontrol() function you write in Page PreRender Event. Please try it. I think it will work. – Sajith A.K. May 27 '14 at 09:14

2 Answers2

1

Finally i found the reason after googling the issue. The issue in this question is a view state.

In asp.net when the page post back it loses the viewstate for the dynamically generated controls. So to get over this issue i recreated the controls in the page load event when it is post back. in that way controls will be added back to the current page and i can find those controls in the button on click event.

thanks all for your time and guidence.

Sachin Trivedi
  • 2,033
  • 4
  • 28
  • 57
0
Panel div = new Panel();
Panel innerdiv = new Panel();
TextBox txt = new TextBox();

innerdiv.Controls.Add(txt);
div.Controls.Add(innerdiv);
CustomAttributes.Controls.Add(div);

Your CustomAttributes holds Panel. Try this :

var textboxes = CustomAttributes.Controls.OfType<Panel>()
    .Select(p => p.Controls.OfType<Panel>().First())
    .Select(p => p.Controls.OfType<TextBox>().First())
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • not working. textboxes is having 0 count. no text box is found. – Sachin Trivedi May 27 '14 at 07:35
  • The above line work, try it right after the `foreach` loop. The problem is that inside another event like `Button.OnClick` a postback is performed rendering the page to be re-initialized(or refreshed); Hence, we lost the dynamic controls. – Xiaoy312 May 27 '14 at 08:30
  • yes i also read some topics on that. do you have any idea how to preserve the controls state so that i can find them in another event like button click ?? – Sachin Trivedi May 27 '14 at 08:45
  • You can recreate the controls on `Page.Load` or on another event handler to keep the controls on the page. Since, `Button.OnClick` will produce a `post` action, you should be able to extract the textboxes' value from the result. Unfortunately, I do not have access to my laptop, to test this out. – Xiaoy312 May 27 '14 at 14:06