1
請注意這裏的母版頁編號網絡使用登錄控制和LoginView成功登錄後,頁面重定向
<asp:LoginView ID="LoginView1" runat="server" ViewStateMode="Disabled">
<AnonymousTemplate>
<ul>
<li><a id="registerLink" runat="server" href="~/LogReg.aspx">Register</a></li>
<li><a id="loginLink" runat="server" href="~/LogReg.aspx">Log in</a></li>
</ul>
</AnonymousTemplate>
<LoggedInTemplate>
<ul>
<li><a id="manage" runat="server" class="username" href="~/Account/Manage.aspx" title="Manage your account">
<asp:LoginName ID="LoginName2" runat="server" CssClass="username" />
</a>
</li>
<li><a id="A2" runat="server" href="~/Logout.aspx">Log Out</a></li>
</ul>
</LoggedInTemplate>
</asp:LoginView>
這是登錄頁面代碼
<asp:Login ID="Login1" runat="server" Height="198px" Width="297px" DisplayRememberMe="False" OnAuthenticate="Login1_Authenticate" UserNameLabelText="Email:">
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<LoginButtonStyle Font-Size="Small" />
<TextBoxStyle Font-Size="0.8em" CssClass="float-right" Width="180px" Height="12px" />
<LabelStyle CssClass="float-left" />
<TitleTextStyle CssClass="task-title" />
</asp:Login>
登錄控制的認證事件看起來像:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string un = Login1.UserName;
string pw = Login1.Password;
var c = db.customers.Where(s => s.customer_email == un && s.customer_password == pw && s.customer_deleted != true);
if (c.Count() == 1)
{
e.Authenticated = true;
//Something to redirect //Response.Redirect(Page.PreviousPage.ToString());
}
else
e.Authenticated = false;
}
問題是,當我在這裏使用Response.Redirect函數時,它成功重定向,但loginView模板返回到anonymousTemplate。如果我不使用重定向,模板將更改爲LoggedInTemplate,但我想將其重定向到上一頁。我使用了destinationPageUrl屬性,但它的行爲是一樣的。感謝您的幫助:)
太棒了!謝謝:) – 2014-09-13 15:46:19
Page.PreviousPage在這個事件中顯示爲null,你能不能請告訴我應該採取什麼措施重定向到上一頁,而不是使用會話? 謝謝,現在明白了。 – 2014-09-13 15:56:15
您可以嘗試** Request.UrlReferrer **。但是,我不推薦任何一個,因爲它們不可靠。相反,我們通常在QueryString **中使用** ReturnUrl。 – Win 2014-09-13 16:01:07