简体   繁体   English

如何从文本框的值添加查询字符串? ASP .NET C#

[英]how to add querystring from value of textbox? ASP .NET c#

I have a login page for a website where members insert their email in a textbox. 我有一个网站登录页面,成员可以在其中将电子邮件插入文本框。 My question is how to redirect to another page depending on the value of the textbox, for example 我的问题是如何根据文本框的值重定向到另一个页面,例如

Response.Redirect("homepage.aspx?and here the value of the textbox");

keyboardP's answer is the correct one, but please keep in mind the security hole this opens in your site. keyboardP的答案是正确的,但是请记住,这会在您的站点中打开安全漏洞。 You need to run that textbox value through all the validation you can to avoid XSS attacks, request hijacking, or any other malicious behavior that can done by simply using the value from a textbox without verifying it. 您需要通过所有可能的验证来运行该文本框值,从而避免XSS攻击,请求劫持或任何其他恶意行为,这些问题仅通过使用文本框中的值而不进行验证即可完成。

If this isn't an issue, you should mark keyboardP's answer as the right one. 如果这不是问题,则应将keyboardP的答案标记为正确的答案。

Response.Redirect(string.Format("homepage.aspx?{0}", myTextbox.Text));

All the code above does is replace {0} with the value of the textbox, which is retrieved using the Text property. 上面的所有代码都用文本框的值替换了{0} ,该文本框是使用Text属性检索的。 The page is then redirected to the newly formed string which contains the textbox value. 然后将页面重定向到包含文本框值的新形成的字符串。

As a side note, since I don't know what's in your textbox, you should add an ID for the querystring like this: 附带说明,由于我不知道您的文本框中包含什么,因此应为querystring添加一个ID,如下所示:

string.Format("homepage.aspx?mytextbox={0}", myTextbox.Text)

This makes it easier to retrieve the QueryString from the other side. 这样可以更轻松地从另一侧检索QueryString。

EDIT - To receive the QueryString, you handle it on the page that it's being redirected to. 编辑-要接收QueryString,请在将其重定向到的页面上对其进行处理。 In that page, you could do something like this: 在该页面中,您可以执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
    //notice that we're using the id `mytextbox`
    string qsvalue = Request.QueryString["mytextbox"];
    if (qsvalue != null)
    {
        ...
    }
}

I'm assuming it's a string, but you can also parse it to other types such as integers if that's what you're expecting. 我假设它是一个字符串,但是如果您期望的话,也可以将其解析为其他类型,例如整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM