Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > get value of dynamic control on postback?

Reply
Thread Tools

get value of dynamic control on postback?

 
 
=?Utf-8?B?RGF2ZQ==?=
Guest
Posts: n/a
 
      08-03-2007
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);
}

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFuaXNoIEJhZm5h?=
Guest
Posts: n/a
 
      08-03-2007
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/re...select=1495875
--
Hope this answers your question.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



"Dave" wrote:

> 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);
> }
>

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      08-03-2007
Place the code for control creting in the PreInit event. The reason is
because LoadViewState occurs between Init and Load events.

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


"Dave" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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);
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
placement of dynamic control depends on VALUE of dynamic control Larry Bud ASP .Net 1 01-10-2007 10:07 PM
How to get dynamic control's value after postback? YesGoGoGo@gmail.com ASP .Net Web Controls 1 04-20-2006 07:12 AM
Help!! how to get the value of dynamic web user control? hb ASP .Net 2 04-13-2004 12:44 AM
Get value of a dynamic control Dominic ASP .Net Web Controls 1 10-20-2003 05:26 AM
How to get value of dynamic DropDownList in User Control ? Piotr Strycharz ASP .Net 7 09-30-2003 11:37 AM



Advertisments