![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||