Solved

ASP.Net save viewstate of dynamic controls on post back

Posted on 2014-03-18
1
542 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 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