I've got a DropDown which I populate from a database. When an item is selected I want to redirect to another page with the selected id, but the problem is it passes "1" no matter what was selected.

protected void Page_Load(object sender, EventArgs e)
    {
        Populate();
    }

    public void Populate()
    {
        OleDbConnection con = new OleDbConnection(
           "Provider=Microsoft.Jet.OLEDB.4.0; " +
           "Data Source=" + Server.MapPath("connect.mdb"));

        con.Open();

        string strQuery = "SELECT A_ID, Title_eng FROM C_about";
        OleDbCommand cmd = new OleDbCommand(strQuery, con);
        OleDbDataReader reader = cmd.ExecuteReader();

        titleSelection.DataSource = reader;
        titleSelection.DataValueField = "A_ID";
        titleSelection.DataTextField = "Title_eng";
        titleSelection.DataBind();

        con.Close();
        con.Dispose();
    }

    protected void titleSelection_SelectedIndexChanged(object sender, EventArgs e)
    {
        string A_ID = titleSelection.SelectedValue.ToString();

        Response.Redirect("aboutEdit.aspx?A_ID=" + A_ID);
    }

this is the view source:
Title:<br />
<select name="titleSelection" onchange="javascript:setTimeout('__doPostBack(\'titleSelection\',\'\')', 0)" id="titleSelection">
<option selected="selected" value="1">WHO WE ARE</option>
<option value="2">WHAT WE DO</option>

<option value="3">Permanent Placements</option>
<option value="4">Temporary staff</option>
<option value="5">Training &amp; Development</option>
<option value="6">CV Writing Tips</option>
<option value="7">Jobs Titles &amp; Descriptions</option>

<option value="8">Looking for Talent?</option>
<option value="9">Looking for a Job</option>
<option value="10">Register</option>

Dani AI

Generated

As pointed out, the dropdown was being rebound on every page load which wiped out the user selection — that is why the page always sent "1". Wrapping the population so it only runs on the initial load preserves the control state and lets SelectedIndexChanged report the actual choice (this is what fixed it for ).

Why this happens: rebinding replaces the Items collection and clears the posted/ viewstate selection, so the event sees the default item. The practical rule is to populate data controls only on the first load unless you intentionally need to refresh them on postbacks.

A few extra, durable tips to avoid related issues and harden the page:

  • Use using blocks for connections and move the connection string to web.config to simplify maintenance and ensure proper disposal.
  • If you add a manual "Select..." option, set AppendDataBoundItems=true so it isn't thrown away by data binding.
  • Ensure the DropDownList has AutoPostBack=true (the onchange in your view source shows it already does).
  • Always validate the incoming query string on aboutEdit.aspx before using it. Example parse pattern:
int id;
if (!int.TryParse(Request.QueryString["A_ID"], out id))
{
    // handle missing/invalid id (show error or redirect)
}

If you ever must rebind on postback, save the current SelectedValue before the bind and restore it afterwards. Also consider binding from a DataTable (so the connection can be closed earlier) and always parameterize queries to avoid injection.

Recommended Answers

All 2 Replies

Hello jellybeannn,
I guess you have to add to Page_Load event couple strings:

protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
            Populate();
    }

It should help you.

commented: Good solve! +1

Thanks man, it solved it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.