I need to fire the Selectedindexchanged event of a dynamically created dropdownlist. I've no code in aspx page. All code are in codebehind.
DropDownList objdropDown = new Dr开发者_如何转开发opDownList();
objdropDown.EnableViewState = true;
objdropDown.AutoPostBack = true;
//objdropDown.AutoPostBack = true;
//objdropDown.SelectedIndexChanged += new EventHandler(objdropDown_SelectedIndexChanged);
protected void objdropDown_SelectedIndexChanged(object sender, EventArgs e)
{
//My code here
}
After selecting a random item drom Dropdownlist, it postbacks but the dropdown control is not visible. What am I doing wrong here? Code would be helpful. Thanks!
Because the code is generated in the code behind it will not be persisted between postbacks, except you explicitly recreate it each time (probably in Page_Load). But then you might have to track the item you selected and set that as selected after each regeneration on postback.
You need to create the drop-down list & bind the event early, in pageInit. It will be then be aware that be able to participate in the postback process & raise events for selectedindexchange.
PageLoad is to late as postback processing has already occurred. Have a look at this: http://msdn.microsoft.com/en-us/library/ms178472.aspx
Did you add the newly created DropDownList to it's parent Container on the Page and set its Visibility to visible?
I have the same issue as this and just wanted to share my resolution for others. Please make sure
- The drop down control ID is the same across all the post backs (better set this to be on the safe side)
- Create the dynamic drop down in the page_Init event.
My issue is relating to the dynamic control ID being changed after post back. My issue is resolved after I make sure that the control ID the same across all post backs.
精彩评论