简体   繁体   English

如何根据登录成功在新窗口/选项卡或同一窗口/选项卡中打开页面?

[英]How to open page in new window/tab or in same window/tab depending on Login success?

I have Login page wich is IFrame. 我的登录页面是IFrame。 If user successfuly logs in i want to open new page with his personal content, and if not I want error message displayed inside same iFrame. 如果用户成功登录,我想用他的个人内容打开新页面,否则,我希望在同一iFrame中显示错误消息。

This is my code: 这是我的代码:

<form class="register-form" runat="server" id='form1' target="_blank">
      <div class="input username">
        <asp:TextBox ID="tbUsername" runat="server" type="text" value="Username"></asp:TextBox>
      </div>
        <div class="input password">
          <asp:TextBox ID="tbPassword" runat="server" value="Password" type="text"></asp:TextBox>
        </div>
      <div class="submit">
        <asp:Button runat="server" ID="btnSignIn" type="button" OnClick="btnSignIn_Click">
        </asp:Button>
      </div>
</form>

As you can see i initially set forms target attribute to "_blank", and plan to change it dynamically. 如您所见,我最初将表单目标属性设置为“ _blank”,并计划对其进行动态更改。

This is my onclick event in code behind: 这是我后面代码中的onclick事件:

protected void btnSignIn_Click(object sender, EventArgs e)
    {
        if (Membership.Providers["CmsMembershipProvider"].ValidateUser(tbUsername.Text, tbPassword.Text))
        {
            Session["Username"] = tbUsername.Text.ToString();
            FormsAuthentication.SetAuthCookie(tbUsername.Text, true);         
            Response.Redirect("../Pages/MyProjects.aspx");
        }
        else {
            btnSignIn.OnClientClick = "openInSameWindow()";
            Msg.Text = "Login failed. Please check your user name and password and try again.";
        }
    }

and this is simple js function: 这是简单的js函数:

    <script type="text/javascript">

        function openInSameWindow() {
            var myForm = document.getElementById('form1');
            myForm.target = '_self';
        }
</script>

Page always opens in new tab/window. 页面总是在新的标签/窗口中打开。 I figured out that form already opens in new page before i set target to "_self" from code behind. 我发现从后面的代码将目标设置为“ _self”之前,该表单已经在新页面中打开。

What is the best solution for this problem? 解决此问题的最佳方法是什么?

Page always opens in new tab/window. 页面总是在新的标签/窗口中打开。 I figured out that form already opens in new page before i set target to "_self" from code behind. 我发现从后面的代码将目标设置为“ _self”之前,该表单已经在新页面中打开。

You are right. 你是对的。 The ASP.net code gets executed AFTER target="_blank" is used for form. 在target =“ _ blank”用于表单之后执行ASP.net代码。 So your setting of target="_self" is wrong. 因此,您对target =“ _ self”的设置是错误的。

One solution for the problem you have is to use POPUP window to show the "../Pages/MyProjects.aspx" page. 解决该问题的一种方法是使用POPUP窗口显示“ ../Pages/MyProjects.aspx”页面。

Otherwise, I would prefer it if you can show "../Pages/MyProjects.aspx" in same window outside IFRAME. 否则,如果您可以在IFRAME之外的同一窗口中显示“ ../Pages/MyProjects.aspx”,我希望使用它。 Otherwise keep the user in iframe showing appropriate message. 否则,请让用户在iframe中显示适当的消息。

For first approach you will have to open the popup when user submits the form ie form.onsubmit handler. 对于第一种方法,您必须在用户提交表单即form.onsubmit处理程序时打开弹出窗口。 And on successful login set the location of POPUP to "Pages/MyProjects.aspx" otherwise close the POPUP. 在成功登录后,将POPUP的位置设置为“ Pages / MyProjects.aspx”,否则关闭POPUP。 Ofcourse don't set target="_blank" 当然不要设置target =“ _ blank”

For second approach also don't set target (ie it should be _self) and on successful login set location like following. 对于第二种方法,也不要设置目标(即应为_self),并且在成功登录后设置位置,如下所示。

window.top.location = "Pages/MyProjects.aspx"

such that your earlier function becomes 这样您以前的功能就变成了

function openInSameWindow() {
  window.top.location = 'Pages/MyProjects.aspx';
}

when there are errors, they will be displayed in iframe only. 出现错误时,它们只会显示在iframe中。

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

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