C# 将项目添加到 DataBound 下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1105149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Item to DataBound Drop Down List
提问by
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.
我有三个下拉列表。第一个是数据绑定以获取不同的实验名称。用户选择,页面回发,第二个下拉菜单显示不同的时间点。这是我需要帮助的地方。我需要向那个下拉列表中添加一个项目,其 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.
我错过了一些东西。
回答by Ahmad Mageed
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
这是一个类似的问题: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)
另一个关于使用此方法的潜在重复项及其解决方法:Dropdownlist AppendDataboundItems(第一项为空)
回答by Ahmad Mageed
<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.
回答by Ryan Versaw
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
这将取代您之前的绑定。
回答by Ben Griswold
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。希望订单有所帮助。
回答by Zoran Perokovic
I suggest using OnDataBound
event of DropDownList
control, and put your code behind there.
That you way you can combine DataSourceID
with code behind.
我建议使用控制OnDataBound
事件DropDownList
,并将您的代码放在那里。这样你就可以DataSourceID
与后面的代码结合起来。
回答by Cua
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;
}
}