Click here to Skip to main content
15,898,036 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: iTextsharp vs pdfbox Pin
gavindon28-Apr-11 8:12
gavindon28-Apr-11 8:12 
GeneralRe: iTextsharp vs pdfbox Pin
Pete O'Hanlon28-Apr-11 8:25
mvePete O'Hanlon28-Apr-11 8:25 
GeneralRe: iTextsharp vs pdfbox Pin
gavindon28-Apr-11 8:34
gavindon28-Apr-11 8:34 
GeneralRe: iTextsharp vs pdfbox Pin
Pete O'Hanlon28-Apr-11 8:36
mvePete O'Hanlon28-Apr-11 8:36 
QuestionUnable To Create Excel Sheet At Runtime In my Application. Pin
Sanket.Patil28-Apr-11 2:37
Sanket.Patil28-Apr-11 2:37 
AnswerRe: Unable To Create Excel Sheet At Runtime In my Application. Pin
Prasanta_Prince28-Apr-11 3:02
Prasanta_Prince28-Apr-11 3:02 
QuestionScorm content viewer Pin
nachavle27-Apr-11 23:05
nachavle27-Apr-11 23:05 
QuestionProblem with cascading DropDownLists in a databound DetailsView Pin
kbalias27-Apr-11 21:30
kbalias27-Apr-11 21:30 
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 ddlClinics.Items.Clear(); that causes the error, the ListItems are just added to the Clinics DropDownList in the DetailsView each time a ListItem is selected in the Firms DropDownList and not replaced with only the relevant ListItems for the selected Firm.

My markup for the DetailsView:


<asp:SqlDataSource ID="SqlDataSource_RTTest" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MyConnectionString1 %>" 
    SelectCommand="SELECT * FROM [RTNumbers] WHERE ([RTID] = @RTID)">
    <SelectParameters>
        <asp:ControlParameter ControlID="HiddenField1" Name="RTID" PropertyName="Value" 
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:HiddenField ID="HiddenField1" runat="server" />


<asp:DetailsView ID="DetailsView2" runat="server" 
    AutoGenerateInsertButton="True" AutoGenerateRows="False" DataKeyNames="RTID" 
    DataSourceID="SqlDataSource_RTTest" Height="50px" Width="125px">
    <EmptyDataTemplate>
        <asp:LinkButton ID="FormViewInsert" runat="server" 
            onclick="DetailsView2_Click">Insert a new RT record</asp:LinkButton>
    </EmptyDataTemplate>
    <Fields>
        <asp:TemplateField HeaderText="Firm" SortExpression="FirmID">
            <InsertItemTemplate>
                <asp:DropDownList ID="ddlFirms_DetailsView" runat="server" AutoPostBack="True"
                    AppendDataBoundItems="True" DataSourceID="SqlDataSource_Firms2" 
                    DataTextField="Firm" DataValueField="FirmID" 
                    SelectedValue='<%# DataBinder.Eval(Container.DataItem,"FirmID")%>' Enabled="True"
                        OnSelectedIndexChanged="ddlFirms_DetailsView_SelectedIndexChanged">
                    <asp:ListItem Value="">- select -</asp:ListItem>
                </asp:DropDownList> 
                <asp:SqlDataSource ID="SqlDataSource_Firms2" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:MyConnectionString1 %>" 
                    SelectCommand="SELECT * FROM [Firms] ORDER BY [Firm]">
                </asp:SqlDataSource>

            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("FirmID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Clinic" SortExpression="ClinicID">
            <InsertItemTemplate>
                <asp:DropDownList ID="ddlClinics_DetailsView " runat="server"
                    AppendDataBoundItems="True" DataSourceID="SqlDataSource_Clinics2" 
                    DataTextField="Clinic" DataValueField="ClinicID" 
                    SelectedValue='<%# DataBinder.Eval(Container.DataItem,"ClinicID")%>' 
                    Enabled="True" AutoPostBack="False">
                    <asp:ListItem Value="">- select -</asp:ListItem>
                </asp:DropDownList> 
                <asp:SqlDataSource ID="SqlDataSource_Clinics2" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:MyConnectionString1 %>" 
                            
                    SelectCommand="SELECT * FROM [Clinics] WHERE ([FirmID] = @FirmID) ORDER BY [Clinic]">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ddlFirms2" Name="FirmID" 
                            PropertyName="SelectedValue" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>

            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("ClinicID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>



And the code-behind:

protected void ddlFirms_DetailsView_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddlClinics = (DropDownList)DetailsView2.FindControl("ddlClinics_DetailsView");

    //this line causes the following error: "has a SelectedValue which is invalid because it does not exist in the list of items."
    ddlClinics.Items.Clear();

    ddlClinics.Items.Add("- select -");
}



If I leave out the ddlFirms_DetailsView_SelectedIndexChanged() event handler then whenever I select a listItem in the ddlFirms_DetailsView for the first time then the correct relevant listItems for that firm is populated in ddlClinics_DetailsView.
However when I select another listItem in ddlFirms_DetailsView the relevant listItems for the new selection is just added to ddlClinics_DetailsView (in other words it also still have the listItems for the previously selected item in ddlFirms_DetailsView as well).

I am a bit stuck here.

Regards.

Kobus
AnswerRe: Problem with cascading DropDownLists in a databound DetailsView Pin
Ali Al Omairi(Abu AlHassan)28-Apr-11 0:45
professionalAli Al Omairi(Abu AlHassan)28-Apr-11 0:45 
QuestionObjectDataSource for DevExpress ASPxGtridView with enabled paging, sorting, grouping, external filters Pin
trongood27-Apr-11 21:15
trongood27-Apr-11 21:15 
AnswerRe: ObjectDataSource for DevExpress ASPxGtridView with enabled paging, sorting, grouping, external filters Pin
Mehul Harry28-Apr-11 14:03
Mehul Harry28-Apr-11 14:03 
Questioninput validation Pin
jashimu27-Apr-11 9:38
jashimu27-Apr-11 9:38 
AnswerRe: input validation Pin
gavindon27-Apr-11 9:49
gavindon27-Apr-11 9:49 
GeneralRe: input validation Pin
jashimu27-Apr-11 10:22
jashimu27-Apr-11 10:22 
GeneralRe: input validation Pin
gavindon27-Apr-11 10:38
gavindon27-Apr-11 10:38 
GeneralRe: input validation Pin
jashimu27-Apr-11 10:51
jashimu27-Apr-11 10:51 
GeneralRe: input validation Pin
Prasanta_Prince28-Apr-11 3:06
Prasanta_Prince28-Apr-11 3:06 
GeneralRe: input validation Pin
jashimu28-Apr-11 10:32
jashimu28-Apr-11 10:32 
AnswerRe: input validation Pin
m@dhu27-Apr-11 18:59
m@dhu27-Apr-11 18:59 
QuestionDeclarative vs Programmatic: opinions Pin
Brad Tumer27-Apr-11 8:55
Brad Tumer27-Apr-11 8:55 
AnswerRe: Declarative vs Programmatic: opinions Pin
Not Active28-Apr-11 2:39
mentorNot Active28-Apr-11 2:39 
GeneralRe: Declarative vs Programmatic: opinions Pin
Prasanta_Prince28-Apr-11 3:09
Prasanta_Prince28-Apr-11 3:09 
AnswerRe: Declarative vs Programmatic: opinions Pin
Renat Khabibulin4-May-11 17:09
Renat Khabibulin4-May-11 17:09 
Questionpdfbox error Pin
gavindon27-Apr-11 8:24
gavindon27-Apr-11 8:24 
QuestionSqlDataSource used in gridpage as well as to databind Pin
vanikanc27-Apr-11 6:00
vanikanc27-Apr-11 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.