Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically created sub-classed dropdownlist not firing event
    primarykey
    data
    text
    <p>There's loads of posts on this subject on the net, but I cant find one that fits my situation;</p> <p>I've have a BasePage class, which my .aspx inherit from; I also have BaseLabel &amp; BaseDDL classes, which extend Label &amp; Dropdownlist respectively. On top of this I have a ReadyDDL class, which combines BaseLabel &amp; BaseDDL into a single control (but this is a class, not a user control) and renders them with their own Div, Table, TableRow, TableCells, &amp; another Label. The ReadyDDL class enables me to define label &amp; dropdownlist &amp; layout in a single html statement as per:</p> <pre><code>&lt;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" /&gt; </code></pre> <p>However I have a problem or two:</p> <p>a) The event doesnt fire. The posts I have read on this subject say that the dropdown must be recreated OnInit &amp; all will be fine. BUT -</p> <p>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</p> <p>ddl.SelectedIndexChanged += new EventHandler(X);</p> <p>because X doesnt exist in the class, only the page. </p> <p>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. </p> <p>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. </p> <p>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?</p> <p>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?</p> <p>Any questions please ask. Any help or suggestions will be appreciated, and I will mark Q as answered if suitable information comes.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload