简体   繁体   中英

Why does my DropDownList not populate with Items from the Page_Load event?

I have this control to fetch some data:

<asp:DropDownList ID="newsOrEvent" runat="server" Text='<%# Eval("newsEvent") %>'></asp:DropDownList>

Then in my Page_Load I have:

protected void Page_Load(object sender, EventArgs e)
        {

            newsOrEvent.Items.clear();
            newsOrEvent.Items.Add("News");
            newsOrEvent.Items.Add("Event");

                    if (!this.IsPostBack)
                    {
                        if(newsID != 0) 
                        {
                            this.BindRepeater();
                           // this.BindImageRepeater();

                        }   

                    }


        }

My compiler is saying: The name 'newsOrEvent' does not exist in the current context

private void BindRepeater()
        {
                using (SqlConnection con = new SqlConnection(constr))
                {
                string myQuery = string.Format("SELECT * FROM tblNewsEvents WHERE newsID = {0}", newsID);


                     using (SqlCommand cmd = new SqlCommand(myQuery, con))
                     {
                        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                        {
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            news.DataSource = dt;
                            news.DataBind();
                        }
                    }
                }
            }

This SO article here suggested:

  • I should .clear the items first
  • Enter the <%# Eval("newsEvent") %> by itself to see if it loads in the page, which it does.

It also said:

If the dropdown controls Text property is assigned with any value before assigning the datasource, this error will occur.

How can I add the items first, before it reads the Eval?

I can not see why a DropDownList does not populate.

As I mentioned in comments asp:DropDownList control doesn't have Text property. The easiest way to get your dropdown filled is to fill it declaratively . Something like this.

<asp:DropDownList ID="ddNewsOrEvents" runat="server" DataTextField="NewsTitle"
    DataValueField="newsID" 
    AppendDataBoundItems="true" DataSourceID="sqlNews">
    <asp:ListItem Value="-1" Text="News"></asp:ListItem>
    <asp:ListItem Value="0" Text="Event"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sqlNews" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionFromWebConfig %>"
    SelectCommand="select newsID, newsTitle from dbo.tblNewsEvents where newsId=@newsID">
    <SelectParameters>
         <%--if newsID comes from some control --%>
        <asp:ControlParameter ControlID="someControl" PropertyName="someProperty" Name="newsID" Type="Int32" />
        <%--if newsId comes from querystring --%>
        <asp:QueryStringParameter QueryStringField="newsid" Name="newsID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

No code behind required. Just follow KISS principle ( K eep I t S imple S tupid)

Follow Up
SelectedValue ( Text ) property of DropdownList control can be set only after the control is DataBound . It means that you can set SelectedValue='<%#Eval("dataField")%>' only if it is in a binding container and DropDownList itself has DataSourceID . Else you have to set .SelectedValue after DataBound event.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM