I have a very simple html form that, upon submission, should redirect the user to another page. There's a field in the form, which value needs to be appended to the redirect Url (via query string) IF the user has entered a value.
我有一个非常简单的html表单,在提交后,应该将用户重定向到另一个页面。表单中有一个字段,如果用户输入了值,则需要将该值附加到重定向Url(通过查询字符串)。
So, if the user enters "hello" in the form, they would be redirected to "mysite.com?x=hello", else it would just direct to "mysite.com?x=". Currently my code is ignoring the IF statement within my POST, can anyone see what the issue is?
因此,如果用户在表单中输入“hello”,它们将被重定向到“mysite.com?x=hello”,否则它将直接指向“mysite.com?x=”。目前我的代码忽略了我的POST中的IF语句,谁能看到问题是什么?
Here's my code:
这是我的代码:
<form>
<input type="text" id="test" name="test" />
<input type="submit" value="Submit" />
</form>
if(IsPost){
var x = Request.Form["test"].IsEmpty() ? false : true;
if(x){
x = "hello";
}
Response.Redirect("http://www.mysite.com?x=" + x);
}
2 个解决方案
#1
2
Your current code won't compile as you try to assign a string ("hello"
) to the boolean x
(true
/ false
), but you only assign it when x
is false, i.e. when Request.Form["test"]
is empty.
当您尝试将字符串(“hello”)分配给布尔值x(true / false)时,您的当前代码将无法编译,但您只在x为false时分配它,即当Request.Form [“test”]为空。
Try this:
尝试这个:
string redirectTo = Request.Form["test"];
if (string.IsNullOrEmpty(redirectTo))
{
redirectTo = "hello";
}
Response.Redirect("http://www.mysite.com?x=" + HttpUtility.UrlEncode(redirectTo));
It's a bit unclear what you want exactly to happen from your question, but you must be able to figure it out from here. If you want to redirect to ?x=
when the input is empty, simply remove the if
block as Request.Form["not entered item"]
will return null
.
从你的问题中你想要发生什么事情有点不清楚,但你必须能够从这里弄明白。如果要重定向到?x =当输入为空时,只需删除if块,因为Request.Form [“not entered item”]将返回null。
As @ChrisK comments on your question, there are better ways of doing this.
正如@ChrisK对您的问题发表评论,有更好的方法可以做到这一点。
#2
1
You're checking IsPost
but it should be IsPostBack
.
你正在检查IsPost,但它应该是IsPostBack。
See this MSDN article for reference.
请参阅此MSDN文章以供参考。
#1
2
Your current code won't compile as you try to assign a string ("hello"
) to the boolean x
(true
/ false
), but you only assign it when x
is false, i.e. when Request.Form["test"]
is empty.
当您尝试将字符串(“hello”)分配给布尔值x(true / false)时,您的当前代码将无法编译,但您只在x为false时分配它,即当Request.Form [“test”]为空。
Try this:
尝试这个:
string redirectTo = Request.Form["test"];
if (string.IsNullOrEmpty(redirectTo))
{
redirectTo = "hello";
}
Response.Redirect("http://www.mysite.com?x=" + HttpUtility.UrlEncode(redirectTo));
It's a bit unclear what you want exactly to happen from your question, but you must be able to figure it out from here. If you want to redirect to ?x=
when the input is empty, simply remove the if
block as Request.Form["not entered item"]
will return null
.
从你的问题中你想要发生什么事情有点不清楚,但你必须能够从这里弄明白。如果要重定向到?x =当输入为空时,只需删除if块,因为Request.Form [“not entered item”]将返回null。
As @ChrisK comments on your question, there are better ways of doing this.
正如@ChrisK对您的问题发表评论,有更好的方法可以做到这一点。
#2
1
You're checking IsPost
but it should be IsPostBack
.
你正在检查IsPost,但它应该是IsPostBack。
See this MSDN article for reference.
请参阅此MSDN文章以供参考。