开发者

Duplicate items in dropdownlist!

开发者 https://www.devze.com 2023-02-02 07:02 出处:网络
Ok I really hope you guys can help me with this, I\'ve spent the past 2 days trying to 开发者_StackOverflow社区figure it out and I think I\'m about to throw my computer out the window, so I thought I

Ok I really hope you guys can help me with this, I've spent the past 2 days trying to 开发者_StackOverflow社区figure it out and I think I'm about to throw my computer out the window, so I thought I might as well ask here first:

I'm designing a web page with two dropdownlists, one for the Make of a car, the other for the Model, both bound to a database with separate SQLDataSources, and using a distinct statement. I add "All" at the top of both by setting appendDataBoundItems = true and adding items named all. Then when I fill the Make with a querystring all the model items get added twice (but only the databound items).

Here's my code:

    <asp:DropDownList ID="DropDownMake" runat="server" 
        DataSourceID="SqlMakes" DataTextField="Make" DataValueField="Make" 
        AppendDataBoundItems="True" EnableViewState="False" AutoPostBack="True">
        <asp:ListItem Selected="True" Value="All">All</asp:ListItem>
    </asp:DropDownList>
    <asp:DropDownList ID="DropDownModel" runat="server" 
        AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlModels" 
        DataTextField="Model" DataValueField="Model" EnableViewState="False">
        <asp:ListItem>All</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlMakes" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT DISTINCT [Make] FROM [Parts]">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlModels" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 


        SelectCommand="SELECT DISTINCT [Model] FROM [Parts] WHERE ([Make] = @Make)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownMake" Name="Make" 
                PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>

'And in the VB file:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    DropDownMake.SelectedValue = Request.QueryString("Make")
    DropDownModel.SelectedValue = Request.QueryString("Model")
End Sub

If I remove the line "DropDownMake.SelectedValue = Request.QueryString("Make")" it doesnt produce duplicates anymore. What is going on?? I swear I've spent more time developing entire iphone apps, if anyone helps figure this out I think I'm going to have to make an award for you.

Thank you!!


I think your databind method might be fired twice. Have you tried to set appendDataBoundItems=false and then attach an event handler for the OnDataBound event and insert your top item in the event handler instead?

protected void yourDdlControl_DataBound(object sender, EventArgs e)
{
    if (yourDdlControl.Items.Count > 0)
    {
            yourDdlControl.Items.Insert(0, "All");
            yourDdlControl.Items[0].Value = "";
            yourDdlControl.SelectedIndex = 0;
        }
    }
}

Ok, so that is c# code, but you should be able to translate it :). Also, you can set the selected value in the DataBound instead based on the existence of your query parameters.


Put your code inside not postback i.e.

If Not Page.IsPostBack Then
     DropDownMake.SelectedValue = Request.QueryString("Make")
     DropDownModel.SelectedValue = Request.QueryString("Model")
End If
0

精彩评论

暂无评论...
验证码 换一张
取 消