I've two Drop Down List Box in a form and need to change the 2nd dropdown lists value on selection of 1st dropdownlist. I've following code

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
        DataFile="c:\dbase.mdb" 
        SelectCommand="SELECT [CcompCode], [compname] FROM [mymain]">
    </asp:AccessDataSource>
    Company Name<asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="AccessDataSource1" DataTextField="compname" 
        DataValueField="CcompCode" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
    <br />
    Financial Year<asp:DropDownList ID="DropDownList2" runat="server" 
        DataSourceID="AccessDataSource2" DataTextField="Fiscalyear" 
        DataValueField="fiscalid">
    </asp:DropDownList>
    
    <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
        DataFile="c:\dbase.mdb" >
    </asp:AccessDataSource>

the 2nd dropdownlist should selection based on company code, it means dropdownlists data value
How to do it Help needed

Dani AI

Generated

Quick, practical answer for : there are two easy approaches — a simple server-side fix (best while you’re learning WebForms) or the AJAX client-side cascading control (smoother UX). ’s CascadingDropDown idea is valid if you want no full-postback; the AJAX Control Toolkit is available via NuGet or the project repo. ()

Minimal server-side solution (no extra toolkit): make the first DropDownList post back, and give the second AccessDataSource a parameter bound to the first list. Put your Access file in App_Data and only bind the company list on initial load. Example markup (new code; adjust table/field names to match your DB):

<asp:DropDownList ID="DropDownList1" runat="server"
    AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    DataSourceID="AccessDataSource1" DataTextField="compname" DataValueField="CcompCode" />

<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/dbase.mdb"
    SelectCommand="SELECT Fiscalyear, fiscalid FROM FiscalTable WHERE CcompCode = ?">
  <SelectParameters>
    <asp:ControlParameter ControlID="DropDownList1" Name="CcompCode" PropertyName="SelectedValue" Type="String" />
  </SelectParameters>
</asp:AccessDataSource>

<asp:DropDownList ID="DropDownList2" runat="server"
    DataSourceID="AccessDataSource2" DataTextField="Fiscalyear" DataValueField="fiscalid" />

Using a ControlParameter in a data-source is the standard pattern; the DropDownList must post back so the parameter updates. Also use Page_Load checks so the first list isn’t rebound on every postback. (learn.microsoft.com)

Minimal code-behind (C#) if you prefer to force a rebind:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList2.DataBind();
}

Troubleshooting checklist: ensure AutoPostBack is set; don’t rebind DropDownList1 on every postback (use if(!IsPostBack)); keep the Access file in ~/App_Data and confirm IIS user can read it; if you want no full postback, use the CascadingDropDown extender or a small jQuery + WebMethod call instead. (learn.microsoft.com)

Recommended Answers

All 3 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.