Solved

ASP.Net save viewstate of dynamic controls on post back

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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