完全披露:我是 .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/