Problem with cascading DropDownLists in a databound DetailsView
-
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
-
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
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.