I am having trouble with an asp:DropDownList. it is binded only if the page is not PostBack (when the page loads first).
But it keeps selecting the first item after postback.
here is some code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
getRegions();
}
private void getRegions()
{
SqlConnection con = new SqlConnection(sqlconnString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select region, emails from Regions", con);
cmd.CommandType = CommandType.Text;
SqlDataReader rdr = cmd.ExecuteReader();
cmbRegion.Items.Clear();
ListItem li = new ListItem("Select region", "Select region");
cmbRegion.Items.Add(li);
while (rdr.Read())
{
li = new ListItem((string)rdr["region"], (string)rdr["emails"]);
cmbRegion.Items.Add(li);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
try this