Last post Apr 08, 2005 12:20 PM by goblyn27
Apr 07, 2005 07:04 PM|goblyn27|LINK
I have a real mess of a problem and I need better, clearer minds than mind to figure it out. I have a custom control that inherits from DropDownList which I call StateBox (suprisingly enough, it is a drop down list with NorthAmerican states/provinces because
I am too lazy to port over the Xml file and redatabind on each site I use this on.)
The problem I am having is that, for reasons I cannot fathom, the control is always firing its SelectedIndexChanged event on Postback whether the SelectedIndex has changed or not. I will provide the code here for you to look at.
ViewState("Abbreviate") = Value.ToString
ViewState("Military") = Value.ToString
ViewState("Mexico") = Value
ViewState("Canada") = Value.ToString
ViewState("Other") = Value.ToString
ViewState("Extended") = Value.ToString
System.Web.HttpContext.Current.Trace.Write("Create Child Controls")
N = Elem(0).ChildNodes(I)
StateCode = N.Attributes("Abbreviation").Value
StateName = N.Attributes("StateName").Value
Region = N.Attributes("Region").Value
include = (
Please please, any insight to why this could be happening would be appreciated. I imagine I am adding the items during the wrong even (CreateChildControls) but I dont know when/where is the right time/place to create them, then.
Thanx In Advance,
--Peter (P.S., I hate that the WYSIWYG adds P tags everytime I hit enter. Thats annoying.)
Apr 07, 2005 09:05 PM|imagemaker|LINK
Because of the way the DropDownList persists an array of its selected items in its view state, I suspect that adding ListItems in CreateChildControls each time the page postbacks may be falsely triggering the SelectedIndexChanged event.
Also, since the Value and Text properties of each ListItem are persisted in view state (unless you have set EnableViewState to false somewhere), you should not have to re-read the XML data and repopulate the Items collection on each postback.
I would suggest moving your code from CreateChildControls to the Load event and wraping it within an IsPostBack check:
Private Sub StateBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then 'create ListItems from XML and add to Items collection End IfEnd Sub
Also, as point of interest (but not the cause of your problem), the DropDownList which inherits from ListControl does not actually create a ChildControls collection - in its RenderContents method it renders the HTML option tags directly
from its collections of ListItems.
Apr 08, 2005 12:20 PM|goblyn27|LINK
Thanks for the advice. I totally fogott about the Load event. Sounds like that will certainly be the winner. Muchas gracias.