c# - SelectedIndexChanged event not firing in child control -
i have asp.net control references user control(popup) when button pressed. when button pressed find drop down list in popup user control , set selected value of it.
protected void btnmybutton_click(object sender, eventargs args) { pnlusercontrol_modalpopupextender.show(); if (ddparent.selectedindex > 0) { dropdownlist dd = ucmycontrol.findcontrolrecursive("ddchild") dropdownlist; dd.selectedvalue = ddparent.selectedvalue; } }
in popup user control i'm expecting selectedindexchanged event fire. however, isn't. ideas why case?
markup child control(only relevant code) below:
<asp:formview id="myform" runat="server" datasourceid="dsmyds" defaultmode="insert"> <edititemtemplate> <asp:panel runat="server"> <table> <tr> <td> <asp:label text="name" runat="server" /></td> <td> <asp:dropdownlist id="ddchild" runat="server" datasourceid="ddds" datamember="" datavaluefield="id" datatextfield="name" selectedvalue='<%# bind("id") %>' autopostback="true" onselectedindexchanged="ddchild_selectedindexchanged" ondatabound="ddchild_databound" /> </td> </tr> </table> <table> <tr> <td> <br /> <asp:button id="btinsertok" runat="server" text="add" commandname="insert" /> <asp:button id="btinsertcancel" runat="server" text="cancel" /> </td> </tr> </table> </asp:panel> </edititemtemplate>
thanks in advance.
it seems event doesn't fire when changing selected value in code-behind. fires when user changes value selecting new value in drop-down.
what do, move code out of event separate method, , call method event. event still fire when user makes change via ui.
protected void ddchild_selectedindexchanged(object sender, eventargs e) { importantstuff(); } public void importantstuff() { // in selectedindexchanged event }
then in button click event, store old selectedindex
value, make change, , compare new selectedindex
value. if they're different, selected index has changed, , can call same method being called selectedindexchanged
event above:
protected void btnmybutton_click(object sender, eventargs args) { pnlusercontrol_modalpopupextender.show(); if (ddparent.selectedindex > 0) { dropdownlist dd = ucmycontrol.findcontrolrecursive("ddchild") dropdownlist; var oldindex = dd.selectedindex; dd.selectedvalue = ddparent.selectedvalue; var newindex = dd.selectedindex; if (oldindex != newindex) // selected index changed importantstuff(); } }
as sam noted, importantstuff()
method should public in usercontrol, can access page usercontrol in.
better yet, assuming "important stuff" doesn't need access other controls in usercontrol, move code separate class, , call both places without requiring page call public methods in usercontrol (your code stays little bit cleaner, , less tangled up).
Comments
Post a Comment