3
votes

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.

6
This should work. Are you getting any compilation error? - SolutionYogi
I'm not getting anything at all. My page loads and the second dropdownlist just says 12 72 192 It should say --Select-- ... 12...72...192. If I use AppendDataBoundItems, it does what i want, but when I change the first dropdown list, then the other time points get added on to the TimePt drop down list. - Stephen

6 Answers

5
votes

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)

3
votes
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            TimePt.DataValueField = "TimePt";
            TimePt.DataTextField = "TimePt";
            var times = TimePTDD.ToList();
            times.Insert(0, new {TimePt="0",TimePt="--Select--"});
            TimePt.DataSource = times;
            TimePt.DataBind();
            //TimePt.SelectedIndex = 0;
        }
    }
0
votes
<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.
0
votes

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.

0
votes

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.

0
votes

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