Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

get value of dynamic control on postback?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave - 03 Aug 2007 04:44 GMT
For simplicity sake,

How do you get the value of selected dropdown on a postback?  I understand
for a dynamic control, you need to re-create it on each postback, but how do
I modify the code to grab the SelectedValue?  

protected void Page_Load(object sender, EventArgs e)
{
       DropDownList list = new DropDownList();
       list.ID = "DropDownList1";
       list.Items.Add(new ListItem("1", "One"));
       list.Items.Add(new ListItem("2", "Two"));
       list.Items.Add(new ListItem("3", "Three"));
       PlaceHolder1.Controls.Add(list);      
}
Manish Bafna - 03 Aug 2007 06:02 GMT
Hi,
So, the controls that were dynamically created are no longer there and
consequently the values returned from these controls have no place to go.
They are lost in the viewstate.

In order to catch these values the dynamically generated controls needs to
be re-generated at Page_Load. The important thing is to assign the same ID to
each control. The ViewState uses the ID property of the Control objects to
reinstate the values.

If you create a dynamic dropdownlist, conditionally, say in response to some
other control's click event. Then, on PostBack, you recreate a "new" dropdown
with the same object name, then "magically" that new dropdown becomes the
dynamically-created dropdown. You get all of the user-initiated and
code-generated properties.
ref:http://www.codeproject.com/aspnet/retainingstate.asp?df=100&forumid=14609&exp=0&
select=1495875

Signature

Hope this answers your question.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

> For simplicity sake,
>
[quoted text clipped - 11 lines]
>         PlaceHolder1.Controls.Add(list);      
> }
Eliyahu Goldin - 03 Aug 2007 07:21 GMT
Place the code for control creting in the PreInit event. The reason is
because LoadViewState occurs between Init and Load events.

Signature

Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin

> For simplicity sake,
>
[quoted text clipped - 11 lines]
>         PlaceHolder1.Controls.Add(list);
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.