views:

3799

answers:

6

Yes, I have read most of the topics here, but I can't find an answer that works.

I have Three drop-down lists. The first is databound to grab distinct experiment names. The user selects, page posts back, and the second drop-down menu displays distinct time points. This is where I need help. I need to add an item to THAT drop-down list whose ID, DataTextField, DataValueField are all TimePt.

Seems simple, but I can't get it to work.

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                TimePt.DataSource = TimePTDD;
                TimePt.DataValueField = "TimePt";
                TimePt.DataTextField = "TimePt";
                TimePt.DataBind();
                TimePt.Items.Insert(0, new ListItem("--Select---", "0"));
                TimePt.SelectedIndex = 0;
            }
        }

I'm missing sometthing.

A: 

Set AppendDataBoundItems="true" on your dropdown list and it should work.

Here's a similar question: How to add Item to SqlDataSource databound list

And another one about potential duplicates using this method and a workaround for it: Dropdownlist AppendDataboundItems (first item to be blank)

Ahmad Mageed
It won't do what I'd like it to do. I tried that and it works, but whenever I go back and choose the first drop down list, I get duplicate values in the second drop down list (which is where I want the "SELECT" to be.That link also doesn't help me because I try it and it says I don't have a method named "MyList" which is what I named it.
Did you intend to use if (Page.IsPostBack) rather than if (!Page.IsPostBack) in your page load?
Ahmad Mageed
I intend to use IsPostBack because when you select the first drop down list, the page loads and the 2nd drop down list populates based on what's selected from the first drop down list.
A: 
<asp:DropDownList ID="ExpAnalysisName" runat="server"
            DataSourceID="DropDownEXP" DataTextField="ExpAnalysisName" 
            DataValueField="ExpAnalysisName" AppendDataBoundItems="true" AutoPostBack=true>
            <asp:ListItem Selected="True" Value="0">Select an Experiment</asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="DropDownEXP" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SGMD3_DataBase %>" SelectCommand="SELECT DISTINCT ExpAnalysisName FROM VW_Data">
        </asp:SqlDataSource>

<asp:DropDownList ID="TimePt" runat="server" AutoPostBack="True" DataSourceID="TimePTDD" DataTextField="TimePt" 
                DataValueField="TimePt">
        </asp:DropDownList>
            <asp:SqlDataSource ID="TimePTDD" runat="server" 
                ConnectionString="<%$ ConnectionStrings:SGMD3_DataBase %>" SelectCommand="SELECT DISTINCT TimePt
    FROM VW_Data
    WHERE ExpAnalysisName = @ExpAnalysisName">
                <SelectParameters>
                    <asp:FormParameter FormField="ExpAnalysisName" Name="ExpAnalysisName" />
                </SelectParameters>
            </asp:SqlDataSource>

So you can have a reference.
A: 

I see that you're specifying the DataSource in two different ways - The DataSourceID in your markup as well as manually setting the DataSource in your codebehind. Try removing the DataSourceID from your markup and see if that helps.

It's been a little while since I've used ASP.NET too much, but I have a feeling your DropDownList is rebinding after the Page_Load which would replace your previous binding.

Ryan Versaw
Yes, that's what I need it to do when I change the first drop down list, but I need "-select-" in the TimePt drop down list without having duplicate values as a result of rebinding that list because the data from the first down menu changed.
This shouldn't generate duplicate values as `AppendDataBoundItems` is not set to true.
Ryan Versaw
No, it shouldn't because I turned it off, but if I turn it on, then it will cause duplicate values, but I will get -- Select -- to stay if I use <asp:ListItem text="--Select--"></asp:ListItem>, but I want this item to appear always, regardless of what I choose from the first drop down menu and then the subsequent items be dynamic based on what was selected :).
The thing is here is that some experiments have three time points, while some have one or two.
A: 

My bet is this is a page lifecycle issue. Per MSDN, each data bound control whose DataSourceID property is set calls its DataBind method.

I think the values are getting bound to the dropdown twice. First, when you manually bind and append the extra item in Page_Load and then the datasource is being bound inside the Page_PreRender event. Try bringing your Page_Load code into Page_PreRender. Hopefully the order helps.

Ben Griswold
A: 

I have encountered the same problem i.e. duplicated items on my cascading dropdownlist. In my scenario, I bind the dropdownlist items using ObjectDataSource. On initial load, it shows duplicated items for drpUser. For postback, I cleared the drpUser items in drpBranch_SelectedIndexChanged(), and it displays fine. Any advice?

First DropDownList

<asp:DropDownList ID="drpBranch" SkinID="Larger" runat="server" AutoPostBack="true" AppendDataBoundItems="true" DataSourceID="odsBranch" DataTextField="BranchDescrip" DataValueField="BranchID">
<asp:ListItem Value="-1">Please Select »</asp:ListItem>

Second DropDownList

<asp:DropDownList ID="drpUser" SkinID="Medium" runat="server" AppendDataBoundItems="true" DataSourceID="odsUserByBranch" DataTextField="Username" DataValueField="UserID">
<asp:ListItem Value="-1">Please Select »</asp:ListItem>

A: 

Correct anwser is:

Use "OnDataBound" event of DropDownList control, and put your code behind there. That you way you can combine DataSourceID with code behind.

Zoran Perokovic