C# – How to get SelectedValue to bother returning the actual selected value

asp.netasp.net-2.0c#-2.0

I'm using the following chunks of code to populate a drop down box on page load from content in an xml file…

foreach (System.Xml.XmlNode item in root.SelectNodes(@"/markers/marker"))
            {
                string tmpValue = item.Attributes["location"].Value + "#" + item.Attributes["lat"].Value + "#" + item.Attributes["lng"].Value;
                destination.Items.Add(new ListItem(item.Attributes["location"].Value, tmpValue));
                tmpNCount++;
            }

I then have a button which I planned to use to perform a search.

protected void qsearch_Click(object sender, EventArgs e)
{

try
{
    string httpPath = Convert.ToString(UrlMaker.httpURL());
    //sysmessage.Text = "TEST" + destination.SelectedValue.ToString();
    string[] Split = destination.SelectedValue.ToString().Split(new Char[] { '#' });
    string tmpLocation = Convert.ToString(Split[0]);
    string tmpLat = Convert.ToString(Split[1]);
    string tmpLon = Convert.ToString(Split[2]);
    string tmpRad = radius.SelectedValue.ToString();


    Response.Redirect(httpPath + "search.aspx?func=longlat&country=gbr&lng="+tmpLon+"&lat="+tmpLat+"&rad="+tmpRad+"&txt="+tmpLocation+"&test=1");
}
catch(Exception ex) { Response.Write("Error on Redirect"); }

}

The Search runs but the bloody thing returns the value of the first item in the dropdown rather than the one I'm selecting.

I'm hopefully missing something really obvious here.

Thanks


Solved

Wiped out the click function as pointless and used the following.

if (IsPostBack)
        {

            try
            {
                string fdata = Request.Form["dest"];
                string[] Split = fdata.ToString().Split(new Char[] { '#' });
                    string tmpLocation = Convert.ToString(Split[0]);
                    string tmpLat = Convert.ToString(Split[1]);
                    string tmpLon = Convert.ToString(Split[2]);
                    string tmpRad = radius.SelectedValue.ToString();                
                Response.Redirect(httpPath + "search.aspx?func=longlat&country=gbr&lng=" + tmpLon + "&lat=" + tmpLat + "&rad=" + tmpRad + "&txt=" + tmpLocation + "&test=1");
            }
            catch(Exception ex) { Response.Write("Error on Redirect" + ex.ToString()); }

        }

Best Answer

At a guess you are clearing the dropdown and calling your code to populate it on every single page load which will result in the first item being selected.

You should make sure this code is within an if(!IsPostBack){ ... }