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.