Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Add Event Handler to dynamic DropDownList??????

Reply
 
Old 04-30-2009   #1 (permalink)
JP


 
 

Add Event Handler to dynamic DropDownList??????

Add Event Handler to dynamic DropDownList

I have a page that contains dynamically generated Dropdown List controls. On
occasion, I want the dynamic dropdown list to perform an AutoPostback. Since
the control is generated programmatically, I bind an event handler to the
control before adding it to the controls collection like sew:
..
..
..
DropDownList controlDropDown = new DropDownList();
controlDropDown.ID = "drop" + drControls["controlName"].ToString();
controlDropDown.AutoPostBack = true;
controlDropDown.SelectedIndexChanged += new
EventHandler(this.controlDropDown_SelectedIndexChanged);
Page.Controls.Add(controlDropDown);
..
..
..
protected void controlDropDown_SelectedIndexChanged(object sender, EventArgs
e)
{
String a = “my answer” //my breakpoint
}

After everything is re-rendered on the post back (Yes I know you have to add
the controls back to the collection on each postback) , the postback occurs
and the controls are re-rendered, but the
controlDropDown_SelectedIndexChanged event does not fire. Why???

--
JP
..NET Software Developer

My System SpecsSystem Spec
Old 04-30-2009   #2 (permalink)
JP


 
 

RE: Add Event Handler to dynamic DropDownList??????

I have solved the issue. DO NOT add the EventHandler when you create the
DropDownList for the first time. Instead add the event handler to the control
after the post back but BEFORE you add the DropDownList back to the controls
collection.

Makes since now since EventHandlers are not maintained in the Control State
or View State. Then in the event method, use the Sender object to reference
what was selected:

String myValue = ((DropDownList)sender).SelectedValue;

Maybe someone else will stumble across this post and find it helpful. It
should work for any control that allows an AutoPostback.


--
JP
..NET Software Developer


"JP" wrote:
Quote:

> Add Event Handler to dynamic DropDownList
>
> I have a page that contains dynamically generated Dropdown List controls. On
> occasion, I want the dynamic dropdown list to perform an AutoPostback. Since
> the control is generated programmatically, I bind an event handler to the
> control before adding it to the controls collection like sew:
> .
> .
> .
> DropDownList controlDropDown = new DropDownList();
> controlDropDown.ID = "drop" + drControls["controlName"].ToString();
> controlDropDown.AutoPostBack = true;
> controlDropDown.SelectedIndexChanged += new
> EventHandler(this.controlDropDown_SelectedIndexChanged);
> Page.Controls.Add(controlDropDown);
> .
> .
> .
> protected void controlDropDown_SelectedIndexChanged(object sender, EventArgs
> e)
> {
> String a = “my answer” //my breakpoint
> }
>
> After everything is re-rendered on the post back (Yes I know you have to add
> the controls back to the collection on each postback) , the postback occurs
> and the controls are re-rendered, but the
> controlDropDown_SelectedIndexChanged event does not fire. Why???
>
> --
> JP
> .NET Software Developer
My System SpecsSystem Spec
Old 05-01-2009   #3 (permalink)
Cor Ligthert[MVP]


 
 

Re: Add Event Handler to dynamic DropDownList??????

JP,

Put a panel between it and everything becomes much easier.

Cor

"JP" <JP@xxxxxx> wrote in message
news:7EB3FCB2-3801-4380-82E4-3FF31970B5A3@xxxxxx
Quote:

> Add Event Handler to dynamic DropDownList
>
> I have a page that contains dynamically generated Dropdown List controls.
> On
> occasion, I want the dynamic dropdown list to perform an AutoPostback.
> Since
> the control is generated programmatically, I bind an event handler to the
> control before adding it to the controls collection like sew:
> .
> .
> .
> DropDownList controlDropDown = new DropDownList();
> controlDropDown.ID = "drop" + drControls["controlName"].ToString();
> controlDropDown.AutoPostBack = true;
> controlDropDown.SelectedIndexChanged += new
> EventHandler(this.controlDropDown_SelectedIndexChanged);
> Page.Controls.Add(controlDropDown);
> .
> .
> .
> protected void controlDropDown_SelectedIndexChanged(object sender,
> EventArgs
> e)
> {
> String a = “my answer” //my breakpoint
> }
>
> After everything is re-rendered on the post back (Yes I know you have to
> add
> the controls back to the collection on each postback) , the postback
> occurs
> and the controls are re-rendered, but the
> controlDropDown_SelectedIndexChanged event does not fire. Why???
>
> --
> JP
> .NET Software Developer
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
DataGridView.DragDrop Event Handler Causes a DataError? (May be a Bug?!?!?!) .NET General
Questions about delegate and event handler .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46