如何正确使用.NET ASP C#中的查询字符串? [英] How to properly use a QueryString in .NET ASP C#?

查看:199
本文介绍了如何正确使用.NET ASP C#中的查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用ASP和C#.NET中Web表单。这种形式有一个的DropDownList 与控制它的的AutoPostBack 属性设置为

I have a web form in .NET using ASP and C#. This form has a DropDownList control with its AutoPostBack property set to True.

当用户选择从的DropDownList

现在,我希望用户能够共享他或只需复制其中有像 http://www.site.com?ProdID=1234 上的查询字符串的URL她的数据。

Now, I want the user to be able to share his or her data by just copying the URL which has the QueryString on it like http://www.site.com?ProdID=1234.

我做了以下尝试做到这一点:

I have done the following to try to accomplish this:

protected void Page_Load(object sender, EventArgs e)
{
    // check if the URL has a QueryString item if yes the use it.
    if (!String.IsNullOrEmpty(Request.QueryString["ProdID"]))
    {
        ddlProduct.SelectedIndex = ddlProduct.Items.IndexOf(ddlProduct.Items.FindByValue(Request.QueryString["ProdID"]));
    }
}



与此代码的问题是,当用户选择从DropDownList中不同的项目他或她选择的项目得到由在Page_Load(),因为我们现在对URL查询字符串覆盖。 (我建立的查询字符串,然后使用重定向()方法在同一页上的事件)

The Problem with this code is that when the user chooses a different item from the DropDownList his or her selected item gets overwritten by the Page_Load() since we have now a QueryString on the URL. (I build the QueryString and then use the Redirect() method to the same page on an Event)

所以在这里我的问题是:是否有可能不断变化的网址动态查询字符串,当用户更改从DropDownList中选定的项目和形式所选项目上显示正确的数据?

So my question here is: Is it possible to keep changing the URL's query string on the fly when the user changes the selected item from the DropDownList and display the proper data on the form for the selected item?

我有一种感觉,这是像鸡还是先有蛋的问题。

I have the feeling that this is like the chicken or the egg problem.

感谢您。

推荐答案

检查网页是否回发到服务器,与您现有的逻辑一起,像这样的:

Check for whether or not the page is posting back to the server, along with your existing logic, like this:

if(!IsPostBack && !String.IsNullOrEmpty(Request.QueryString["ProdID"]))
{
    ddlProduct.SelectedIndex = ddlProduct.Items.IndexOf(ddlProduct.Items.FindByValue(Request.QueryString["ProdID"]));
}

这篇关于如何正确使用.NET ASP C#中的查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆