将项添加到DataBound下拉列表

时间:2021-06-27 15:55:11

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.

我有三个下拉列表。第一个是数据绑定以获取不同的实验名称。用户选择,页面回发,第二个下拉菜单显示不同的时间点。这是我需要帮助的地方。我需要在THAT下拉列表中添加一个项目,其ID,DataTextField,DataValueField都是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 个解决方案

#1


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

在下拉列表中设置AppendDataBoundItems =“true”,它应该可以工作。

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

这是一个类似的问题:如何将Item添加到SqlDataSource数据绑定列表

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

另一个关于使用此方法的潜在重复项及其解决方法:Dropdownlist AppendDataboundItems(第一项为空白)

#2


    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;
        }
    }

#3


<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.

#4


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.

我看到你以两种不同的方式指定DataSource - 标记中的DataSourceID以及在代码隐藏中手动设置DataSource。尝试从标记中删除DataSourceID,看看是否有帮助。

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.

这已经有一段时间了,因为我太多地使用了ASP.NET,但我感觉你的DropDownList在Page_Load之后重新绑定,它将替换你以前的绑定。

#5


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

我敢打赌,这是一个页面生命周期问题。根据MSDN,每个设置了DataSourceID属性的数据绑定控件都会调用其DataBind方法。

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.

我认为价值观将两次下降到下拉列表。首先,当您手动绑定并附加Page_Load中的额外项目,然后数据源被绑定在Page_PreRender事件中。尝试将Page_Load代码放入Page_PreRender。希望订单有所帮助。

#6


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

我建议使用DropDownList控件的OnDataBound事件,并将你的代码放在那里。您可以将DataSourceID与代码组合在一起。

#1


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

在下拉列表中设置AppendDataBoundItems =“true”,它应该可以工作。

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

这是一个类似的问题:如何将Item添加到SqlDataSource数据绑定列表

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

另一个关于使用此方法的潜在重复项及其解决方法:Dropdownlist AppendDataboundItems(第一项为空白)

#2


    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;
        }
    }

#3


<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.

#4


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.

我看到你以两种不同的方式指定DataSource - 标记中的DataSourceID以及在代码隐藏中手动设置DataSource。尝试从标记中删除DataSourceID,看看是否有帮助。

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.

这已经有一段时间了,因为我太多地使用了ASP.NET,但我感觉你的DropDownList在Page_Load之后重新绑定,它将替换你以前的绑定。

#5


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

我敢打赌,这是一个页面生命周期问题。根据MSDN,每个设置了DataSourceID属性的数据绑定控件都会调用其DataBind方法。

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.

我认为价值观将两次下降到下拉列表。首先,当您手动绑定并附加Page_Load中的额外项目,然后数据源被绑定在Page_PreRender事件中。尝试将Page_Load代码放入Page_PreRender。希望订单有所帮助。

#6


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

我建议使用DropDownList控件的OnDataBound事件,并将你的代码放在那里。您可以将DataSourceID与代码组合在一起。