Solved

ASP.Net save viewstate of dynamic controls on post back

Posted on 2014-03-18
1
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 93
ASP.NET MVC - Views 3 59
ASP.NET Load html part first, then start function 2 37
ASP.NET VB code for check and insert in a table 4 42
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

739 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