[英]Form self-submission
所以我試圖讓一個頁面提交給自己,然后在出錯時或者在成功的其他地方重定向回自身。
我有在VB.NET中工作的示例代碼,但我正在嘗試使用相同的代碼在C#中工作。 我覺得錯誤是在使用Page_Load - 我覺得我應該使用另一個電話。
由於代碼現在,我得到一個無限重定向循環,這是不可接受的。
這是代碼:
<% @Page Language="C#" Debug="true" %>
<% @Import Namespace="System.Web" %>
<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e) {
if( Request.Form["username"] == "admin" && Request.Form["password"] == "password") {
HttpContext.Current.Session["username"] = Request.Form["username"];
HttpContext.Current.Session["password"] = Request.Form["password"];
Response.Redirect("elsewhere.html");
}
else {
Response.Redirect("login.aspx?errors=1");
}
}
</script>
<!-- #include file="header.html" -->
<form action="" method="post">
<div id="errors">Incorrect Username or Password</div>
<div><span>Username:</span><input name="username" /></div>
<div><span>Password:</span><input name="password" type="password" /></div>
<div><input type="button" value="Login" id="loginbutton" /></div>
</form>
<!-- #include file="footer.html" -->
謝謝!
我想你錯過了
if (IsPostBack) {
//Your code here
}
這將允許您的代碼僅在表單剛剛提交時觸發。
無限重定向的原因是
Response.Redirect("login.aspx?errors=1");
當第一次加載頁面時,Request.Form [“username”] ==“admin”,這些條件中的任何一個都會導致else部分執行。 它無限地一次又一次地加載login.aspx。
當頁面回發時我們將這些語句執行。 你的代碼就是。
if(Page.IsPostBack)
{
if( Request.Form["username"] == "admin" && Request.Form["password"] == "password") {
HttpContext.Current.Session["username"] = Request.Form["username"];
HttpContext.Current.Session["password"] = Request.Form["password"];
Response.Redirect("elsewhere.html");
}
else {
Response.Redirect("login.aspx?errors=1");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.