Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with cascading DropDownLists in a databound DetailsView

Problem with cascading DropDownLists in a databound DetailsView

Scheduled Pinned Locked Moved ASP.NET
helpcsharphtmlasp-netsysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    Hi I am trying to develop a website using ASP.NET 4.0 and Visual Web Developer 2010 Express. I am struggling with cascading DropDownLists in a DetailsView when the DetailsView is bound to a SqlDataSource. I have tried the logic outside the DetailsView and the cascading DropDownLists work fine. The markup for the DropDownLists outside the DetailsView is as follows:

    <asp:DropDownList ID="ddlFirms" runat="server" AutoPostBack="True"
    AppendDataBoundItems="True" DataSourceID="SqlDataSource_Firms1" DataTextField="Firm"
    DataValueField="FirmID"
    onselectedindexchanged="ddlFirms_SelectedIndexChanged">
    <asp:ListItem Value="">- select -</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource_Firms1" runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString1 %>"
    SelectCommand="SELECT * FROM [Firms] ORDER BY [Firm]"></asp:SqlDataSource>

    <asp:DropDownList ID="ddlClinics" runat="server"
    AppendDataBoundItems="True" DataSourceID="SqlDataSource_Clinics1" DataTextField="Clinic"
    DataValueField="ClinicID">
    <asp:ListItem Value="">- select -</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource_Clinics1" runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString1 %>"
    SelectCommand="SELECT * FROM [Clinics] WHERE ([FirmID] = @FirmID) ORDER BY [Clinic]">
    <SelectParameters>
    <asp:ControlParameter ControlID="ddlFirms" Name="FirmID"
    PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>

    And the code-behind:

    protected void ddlFirms_SelectedIndexChanged(object sender, EventArgs e)
    {
    //this is to clear the list in ddlClinics before it is repopulated with the relevant list for the selected
    //item in ddlFirms
    ddlClinics.Items.Clear();
    //without this line the initial value was not added
    ddlClinics.Items.Add("- select -");
    }

    This works fine. When a listItem is selected in the ddlFirms, only the relevant listItems for that firm is populated in ddlClinics. When I try it in the DetailsView it is not so straight-forward. The problem is that if I try to do the logic exactly as for when the DropDownLists which are not in a DetailsView, it gives me the error: "has a SelectedValue which is invalid because it does not exist in the list of items." When I remove the line ddlCl

    A 1 Reply Last reply
    0
    • K kbalias

      Hi I am trying to develop a website using ASP.NET 4.0 and Visual Web Developer 2010 Express. I am struggling with cascading DropDownLists in a DetailsView when the DetailsView is bound to a SqlDataSource. I have tried the logic outside the DetailsView and the cascading DropDownLists work fine. The markup for the DropDownLists outside the DetailsView is as follows:

      <asp:DropDownList ID="ddlFirms" runat="server" AutoPostBack="True"
      AppendDataBoundItems="True" DataSourceID="SqlDataSource_Firms1" DataTextField="Firm"
      DataValueField="FirmID"
      onselectedindexchanged="ddlFirms_SelectedIndexChanged">
      <asp:ListItem Value="">- select -</asp:ListItem>
      </asp:DropDownList>
      <asp:SqlDataSource ID="SqlDataSource_Firms1" runat="server"
      ConnectionString="<%$ ConnectionStrings:MyConnectionString1 %>"
      SelectCommand="SELECT * FROM [Firms] ORDER BY [Firm]"></asp:SqlDataSource>

      <asp:DropDownList ID="ddlClinics" runat="server"
      AppendDataBoundItems="True" DataSourceID="SqlDataSource_Clinics1" DataTextField="Clinic"
      DataValueField="ClinicID">
      <asp:ListItem Value="">- select -</asp:ListItem>
      </asp:DropDownList>
      <asp:SqlDataSource ID="SqlDataSource_Clinics1" runat="server"
      ConnectionString="<%$ ConnectionStrings:MyConnectionString1 %>"
      SelectCommand="SELECT * FROM [Clinics] WHERE ([FirmID] = @FirmID) ORDER BY [Clinic]">
      <SelectParameters>
      <asp:ControlParameter ControlID="ddlFirms" Name="FirmID"
      PropertyName="SelectedValue" Type="Int32" />
      </SelectParameters>
      </asp:SqlDataSource>

      And the code-behind:

      protected void ddlFirms_SelectedIndexChanged(object sender, EventArgs e)
      {
      //this is to clear the list in ddlClinics before it is repopulated with the relevant list for the selected
      //item in ddlFirms
      ddlClinics.Items.Clear();
      //without this line the initial value was not added
      ddlClinics.Items.Add("- select -");
      }

      This works fine. When a listItem is selected in the ddlFirms, only the relevant listItems for that firm is populated in ddlClinics. When I try it in the DetailsView it is not so straight-forward. The problem is that if I try to do the logic exactly as for when the DropDownLists which are not in a DetailsView, it gives me the error: "has a SelectedValue which is invalid because it does not exist in the list of items." When I remove the line ddlCl

      A Offline
      A Offline
      Ali Al Omairi Abu AlHassan
      wrote on last edited by
      #2

      Sir, i think that you need to add an update panel so the content of the dropdown list could be changed. 100 :rose:

      Help people,so poeple can help you.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups