Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Querystring to Set Selection in Dropdown List

Full disclosure: I'm very new to .NET and sort of feeling my way through it. Been asked to make a tweak and I'm not sure exactly where to start. Was hoping someone might be able to provide a helpful link or example. Would be greatly appreciated.

Basically, I'd like to read in a querystring... let's call it "inquirytype". If that querystring is equal to "other", I want to change the selection in a dropdown box that I have in my .ascx control (see below):

<asp:DropDownList ID="inquiry_type" runat="server" CssClass="inquiry_type">
   <asp:ListItem Value="" Selected="True">Select Below</asp:ListItem>
   <asp:ListItem>Place an Order</asp:ListItem>
   <asp:ListItem>Order Status</asp:ListItem>
   <asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>

Is there a way I can keep this code in my .ascx file and still achieve this by adding something to my .cs file? Or must I create a function in my .cs that creates this dropdown list altogether?

Thanks in advance!

like image 340
ndisdabest Avatar asked Feb 22 '23 15:02

ndisdabest


1 Answers

Try something like this:

DropDownList1.SelectedValue = Request.QueryString["foo"];

You can also do it like this:

ListItem item = DropDownList1.Items.FindByValue(Request.QueryString["foo"]);
if (item != null)
{
    item.Selected = true;
}

I don't think you'll need to test for null, but incase you do:

DropDownList1.SelectedValue = Request.QueryString["foo"] ?? String.Empty;
like image 153
James Johnson Avatar answered Mar 03 '23 16:03

James Johnson