c# - dropdownlist Select from SQL database -
i'm trying project car maintenance, , have small problem. project like:
4 kind of maintenance plans different cars.
cara ( mirrora, etc etc ) carb ( mirrorb, etc etc ) carc ( mirrorc, etc etc ) card ( mirrord, etc etc ) what i'm trying when chose car (from dropdownlist), program selects right maintenance plan car!
sqlcommand cmd = new sqlcommand("select id, description accauto_maps", con); con.open(); dropdownlist1.datasource = cmd.executereader(); dropdownlist1.datatextfield = "description"; dropdownlist1.datavaluefield = "id"; dropdownlist1.databind(); now i'm stuck.
now need set selectedvalue in list and/or define event onselectedindexchanged process user's selection
below example how did it:
<asp:dropdownlist id="ddlstatus" autopostback="true" runat="server" datasourceid="edslookstatus" datavaluefield="cd" datatextfield="lookupname" onselectedindexchanged="ddlstatus_selectedindexchanged" /> <asp:textbox id="txtbxstatus" runat="server" text='<%# bind("status") %>' visible="false" /> here trick: added invisible textbox, linked "status". when ddl change value set new value txtbx:
protected void ddlstatus_selectedindexchanged(object sender, eventargs e) { dropdownlist ddl = formviewclient.findcontrol("ddlstatus") dropdownlist; textbox txtbx = formviewclient.findcontrol("txtbxstatus") textbox; if (ddl != null && txtbx != null) { txtbx.text = ddl.selectedvalue; } } to set selected value:
<asp:formview ... ondatabound="formviewclient_databound" > and
protected void formviewclient_databound(object sender, eventargs e) { ... dropdownlist ddl = formviewclient.findcontrol("ddlstatus") dropdownlist; textbox txtbx = formviewclient.findcontrol("txtbxstatus") textbox; if (ddl != null && txtbx != null) { ddl.selectedvalue = txtbx.text; } ... }
Comments
Post a Comment