Friday, February 12, 2010

ASP .NET - DropDown control's SelectedIndexChanged event doesn't seem to fire?

ASP .NET - DropDown event handling?


I have a DropDown control (with AutoPostBack = true) which I'm trying to use to make a couple of other controls Visible when a specific value is selected.





The controls I want to manipulate the visibility with on the .ASPX page don't have their Visible property set to any default value, so they start out as being Visible. In the code behind, I have statements in Page_Load to hide them.





I'm using the SelectedIndexChanged event, and the handler looks something like this:





if (myDropDownBox.Text.Trim().Contains(';Tem鈥?br>

{


lblDNCTemporaryExpirationDate.Visible = true;


TDNCCalendar.Visible = true;


}


else


{


lblDNCTemporaryExpirationDate.Visible = false;


TDNCCalendar.Visible = false;


}





However, when I make the selection which should cause the other controls to become Visible, they never do.





Should I be using a different event handler?! Or is there a better method?!ASP .NET - DropDown control's SelectedIndexChanged event doesn't seem to fire?
Make sure that in the Page_Load event, you have If(!isPostBack){} around the code that hides your controls.





On a side note, I'd recommend doing this via javascript on the client, if it's at all possible. I'm very anti-postback these days.





Edit: Here's some information that you can use. I haven't written much ASP.Net since my last job, so I don't have any code available, but this should get you started. http://msdn2.microsoft.com/en-us/library鈥?/a>





In javascript, the dropdown will be called a ';select'; and you'll use its change event. The other controls will have .style.visibility = 'hidden', or style.display = 'none', depending on whether you want the hidden controls to take up any space.ASP .NET - DropDown control's SelectedIndexChanged event doesn't seem to fire?
GOOGLE IT
Check to see if the asp:DropDownList has AutoPostBack=True in its declaration. If it doesn't, then add it, as otherwise the events won't fire.





If it does have, then but some breakpoints in the PageLoad and the onselected index changed methods... as well as other key method which may have something to do with it, and hit F5 in visual studio.





This will enter debug mode.

No comments:

Post a Comment