C# 如何从asp中的查询字符串中获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/646365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get data from the query string in asp?
提问by jergason
I am working on a project for an IT class where I need to pass in a value on the query string in a php page and read it into a hidden field on an ASP page.
我正在为一个 IT 类项目工作,我需要在 php 页面中的查询字符串上传递一个值,并将其读入 ASP 页面上的隐藏字段。
I currently am passing the parameter fine from the php page to ASP, but I am pretty new to .NET in general. How do I get the data out of the string and into a variable in C#? For example, if the url is blah.com/upload?username=washington
, how would I get "washington" and save it into a hidden field? Thanks a ton.
我目前正在将参数从 php 页面传递到 ASP,但总的来说我对 .NET 还很陌生。如何将数据从字符串中取出并放入 C# 中的变量中?例如,如果 url 是blah.com/upload?username=washington
,我将如何获得“华盛顿”并将其保存到隐藏字段中?万分感谢。
Jergason
杰格森
Edit
编辑
我知道这会很容易。万分感谢。采纳答案by Darin Dimitrov
You can add a hidden field in your aspx file:
您可以在 aspx 文件中添加一个隐藏字段:
<asp:HiddenField ID="username" runat="server" />
And in your code behind populate it from the request parameter:
并在后面的代码中从请求参数填充它:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
username.Value = Request["username"];
}
}
回答by Noldorin
It seems you just want:
看来你只是想要:
string username = Request.QueryString["username"];
回答by Canavar
This returns value from formelements :
这从表单元素返回值:
string username = Request.Form["username"];
This returns value from querystring:
这从querystring返回值:
string username = Request.QueryString["username"];
This looks bothform and querystring collections :
这看起来既是表单又是查询字符串集合:
string username = Request["username"];
回答by fatemeh
if session ("blnIsuSERGOOD") = False or is null (session ("blnISuSERGoo")) = True then
Response.Redirect.Querystring("name")