使用查询字符串在下拉列表中设置选择

ndi*_*est 4 .net c# forms asp.net

完全披露:我对.NET非常陌生,并且感觉很顺利.被要求做一个调整,我不确定从哪里开始.希望有人能够提供有用的链接或示例.非常感谢.

基本上,我想读一个查询字符串......我们称之为"inquirytype".如果该查询字符串等于"other",我想在我的.ascx控件中的下拉框中更改选择(见下文):

<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>
Run Code Online (Sandbox Code Playgroud)

有没有办法可以将此代码保存在我的.ascx文件中,并通过向我的.cs文件添加内容来实现此目的?或者我必须在我的.cs中创建一个完全创建此下拉列表的函数吗?

提前致谢!

Jam*_*son 7

尝试这样的事情:

DropDownList1.SelectedValue = Request.QueryString["foo"];
Run Code Online (Sandbox Code Playgroud)

你也可以这样做:

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

我不认为你需要测试null,但是你要这样做:

DropDownList1.SelectedValue = Request.QueryString["foo"] ?? String.Empty;
Run Code Online (Sandbox Code Playgroud)