I have been searching many forums including this one and not found the answer to this anywhere (sorry if it has been and I have missed it)

I have a drop-down list in my website. The user can use this to browse the categories and then select a product. Once a product is selected, I have a link from that page to go back to the categories page: <a href="thispage.aspx?qstring=<%# Eval("ID") %>">

It sends the category id which works fine. What I was wandering, is it possible to data-bind the drop-down list to check for a qstring and then set the drop down list to the relevant category. (I can't just use javascript to go back as I need the page to be refreshed)


7 Years
Discussion Span
Last Post by ptemedia

if you have a Category then you can do something like this

DropDownList1.SelectedItem = "I am a Default Selection";

Thanks for that. I have it completely working now and thought I'd share for anyone else who has this problem.

1. Set defaultvalue as string
2. The code is in page load so it checks it every time the page is loaded or refreshed.
3. if (!Page.IsPostBack). This means that the code is ignored if the page is a postback which means the listview can be changed despite the 'old' qstring.
4. if (Request.QueryString["qstring"] != null). This is just because an error is thrown if there is no qstring. So this ignores the code if there is no qstring.

Hope this helps someone!

string defaultvalue;

    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            if (Request.QueryString["qstring"] != null)
                defaultvalue = Request.QueryString["qstring"].ToString();
                DropDownList.SelectedValue = defaultvalue;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.