Solved

ASP.Net save viewstate of dynamic controls on post back

Posted on 2014-03-18
1
529 Views
Last Modified: 2014-03-19
Hi..
I have a page in which I'm adding controls dynamically during PAGE_INIT.
The controls are comboboxes in which a user selects a value.
I have a button that is clicked to search. Which calls code behind.
But my values get reset on POSTBACK. How do I enable viewstate on my dynamic controls.
So I can pass the selected values to the seacrh method.
thx
0
Comment
Question by:JElster
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39936756
You don't technically need to use the ViewState. You can simply rely on the fact that the values are posted back in the Request.Forms collection. So, say you do something like the following:
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            DropDownList ddl = new DropDownList();
            ddl.ID = "NewDropDown";
            ddl.Items.Add(new ListItem("One", "1"));
            ddl.Items.Add(new ListItem("Two", "2"));

            this.Form.Controls.Add(ddl);
        }

Open in new window

You can retrieve the selected value on postback with something like:
        protected void Button1_Click(object sender, EventArgs e)
        {
            DropDownList ddl = (this.Form.FindControl("NewDropDown") as DropDownList);
            string id = ddl.UniqueID;

            if (Request.Form[id] != null)
            {
                string value = Request.Form[id].ToString();
            }
        }

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question