Adding an item to the DataBound drop-down list

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

I have three dropdowns. The first is data binding to capture individual experiment names. The user selects, the pages go back, and in the second drop-down menu, individual time points are displayed. I need help here. I need to add an item to the THAT drop-down list whose identifier, DataTextField, DataValueField is TimePt.

It 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 am missing something.

+3
source share
6 answers

AppendDataBoundItems="true" , .

: SqlDataSource

: Dropdownlist AppendDataboundItems ( )

+5
    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.
0

, DataSource - DataSourceID , DataSource . DataSourceID , .

, ASP.NET, , DropDownList Page_Load, .

0

- . MSDN, , DataSourceID, DataBind.

, . -, Page_Load, Page_PreRender. Page_Load Page_PreRender. , .

0

I suggest using OnDataBounda control event DropDownListand putting your code there. What you can combine DataSourceIDwith the code behind.

0
source

Source: https://habr.com/ru/post/1712219/


All Articles