0

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.

davvv
  • 772
  • 1
  • 6
  • 24

1 Answers1

0

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 (Keep It Simple Stupid)

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.

Alex Kudryashev
  • 9,120
  • 3
  • 27
  • 36
  • 1
    Yes it does have [Text](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist(v=vs.110).aspx) because it inherits `ListControl`. – CodingYoshi Sep 09 '17 at 22:58
  • Sorry, `Text` property really exists (inherited) and, for dropdown is the same as `SelectedValue`. – Alex Kudryashev Sep 09 '17 at 23:05