Dynamically created sub-classed dropdownlist not firing event

606 Views Asked by At

There's loads of posts on this subject on the net, but I cant find one that fits my situation;

I've have a BasePage class, which my .aspx inherit from; I also have BaseLabel & BaseDDL classes, which extend Label & Dropdownlist respectively. On top of this I have a ReadyDDL class, which combines BaseLabel & BaseDDL into a single control (but this is a class, not a user control) and renders them with their own Div, Table, TableRow, TableCells, & another Label. The ReadyDDL class enables me to define label & dropdownlist & layout in a single html statement as per:

<moc:ReadyDDL ID="Person" runat="server" Member="@UserID" Caption="Create strike for" 
  DataSourceSQL="SELECT ID, UserName FROM [User] WHERE isDeleted = 0 AND ClientID = 3" TextField="UserName" ValueField="ID"
  OnSelectedIndexChanged="ddl_SelectedIndexChanged" />

However I have a problem or two:

a) The event doesnt fire. The posts I have read on this subject say that the dropdown must be recreated OnInit & all will be fine. BUT -

I'm not dynamically creating a dropdownlist, but a custom extension of one - thus the code which creates the dropdownlist isnt in my aspx, where the event handler is defined, but is in a separate .cs file and accordingly, I cannot write

ddl.SelectedIndexChanged += new EventHandler(X);

because X doesnt exist in the class, only the page.

The only way I've found to get around this is to expose a string property (OnSelectedIndexChanged) which sets another property in BaseDDL, and when BaseDDL is rendered, to add the OnSelectedIndexChanged property to the markup produced.

The html produced looks ok, and on screen it looks ok, and it does postback when I change the selection in the control, but the eventhandler doesnt fire: it currently just contains a couple of assignment statements, which I have a breakpoint on, and which isnt reached.

On reflection, I suppose, rendering the handler only adds the event to the control in so far as the client is concerned, and the server doesnt know about it - but how can I overcome this, and define the handler at control initialisation, when the handler isnt in the same source code file as the initialisation code?

Does anyone have any ideas on either (1) getting the event to fire, or (2) how I can define the event in code, rather than via rendering?

Any questions please ask. Any help or suggestions will be appreciated, and I will mark Q as answered if suitable information comes.

b) the selected value is lost on postback. I know I have to do something with Viewstate, but I havent figured out just what, yet. If you know how I can implement a solution to this, a short example would be much appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

Appears that your are developing a composite control - the correct way to go about this is to inherit from CompositeControl class and override CreateChildControls to add your child controls. This method is called by ASP.NET early in life-cycle and that would eliminate your view-state related issues.

See this article for developing composite control. For event, string typed property is not going to work - you must define the event at your composite control level. You can bubble up the child's event by raising your own event in the handler (this is shown in the article). Another way would be short-circuit the event handlers. For example, define the event in your composite control such as

public event EventHandler SelectedIndexChanged
{
   add
   {
      childDdl.SelectedIndexChanged += value;
   }
   remove
   {
      childDdl.SelectedIndexChanged -= value;
   }
}

childDll is reference to your child ddl control.