0

I am creating a server control (inheriting from CompositeControl).

The control comprises of several data bound drop down lists (and some textboxes etc).

The drop down lists form a hierarchy, so are dependent on each other... eg

ddlCountry

-ddlCounty

--ddlCity

When ddlCountry is selected, this should refresh the list of ddlCounty... and so on with the ddlCity etc.

I would like to access the value of the ddlCountry within the control, so I can set the data source for second ddl (ddlCounty) accordingly, but in CreateChildControls() the viewstate does not seem to have loaded the ddlCountry user selection yet into the control, so I get an empty string.

I also need to provide these ddl values as a public property.

This is getting quite messy, with the use of ReCreateChildControls() etc... which I'm sure is wrong.

My Question: Can anyone suggest the correct pattern / set of rules to abide by to create this server control

eg

  1. initialise controls OnInit
  2. DataBind on OnLoad
  3. Put EnsureChildControls() at beginning of any property: get or set

I assume there must be a correct way of implementing this, but cannot find any documentation or example to get this working as you would expect of a standard control.

Thanks

Community
  • 1
  • 1
Craig
  • 849
  • 8
  • 21
  • Is this webforms? If so just populate ddlCounty datasource in ddlCountry's onSelectedIndexChanged (I think it's called) event and likewise with ddlCity on ddlCounty. I wouldn't go into CreateChildControls or similar for something like this. – Allan S. Hansen Jan 29 '15 at 11:36
  • Thanks Allan, Yes this is webforms... I will do that re: the ddl's. The reason for asking this question was more focused on the server control construction iteslf... to try and find a template / pattern for the whole control. I have tried some trial and error, regarding retriveing the data from the control after the viewstate has been applied, but I cannot seem to get consistent results... and have the correct properties maintained by the control and available on the Page_Load event which contains the control. – Craig Jan 29 '15 at 11:48
  • Dynamic controls are always nasty in webforms, so I try to avoid it :) – Allan S. Hansen Jan 29 '15 at 12:49

0 Answers0