c# - 使用查询字符串设置下拉列表中的选择

标签 c# asp.net .net forms

完全披露:我是 .NET 的新手,并且有点摸不着头脑。被要求进行调整,但我不确定从哪里开始。希望有人能够提供有用的链接或示例。将不胜感激。

基本上,我想读入一个查询字符串...我们称它为“inquirytype”。如果该查询字符串等于“其他”,我想更改我在 .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>

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

提前致谢!

最佳答案

尝试这样的事情:

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

你也可以这样做:

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

我认为您不需要测试 null,但万一您这样做:

DropDownList1.SelectedValue = Request.QueryString["foo"] ?? String.Empty;

关于c# - 使用查询字符串设置下拉列表中的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7822813/

相关文章:

c# - 使用 Razor 将字符串转换为 `cshtml` 文件中的日期时间?

ASP.net Ajax 和 JQuery - 部分回发

asp.net - 无法为 Elmah 配置邮件

c# - MonoTouch 从 subview 设置位置

c# - 无法通过查询字符串传递 # 字符

c# - 返回该方法接受的相同对象有什么问题吗?

c# - Android 支持库 v7 appcompat 不完整

c# - 对象不能从 DBNull 转换为其他类型

.net - 带有 .NET Core 应用程序的 Google API

c# - 检查对象是否为 C# 中的非特定泛型类型