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.
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)
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;
}
}
<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.
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.
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.
I suggest using OnDataBound
event of DropDownList
control, and put your code behind there. That you way you can combine DataSourceID
with code behind.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.