ndisdabest
ndisdabest

Reputation: 329

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!

Upvotes: 4

Views: 7034

Answers (2)

eduar01
eduar01

Reputation: 11

if (Drodownlist.Text == "") {

     //here  fill if is empty
     //lleno si esta vacio


        }

Upvotes: 1

James Johnson
James Johnson

Reputation: 46077

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;

Upvotes: 7

Related Questions