如何在javascript代码中添加会话?

时间:2014-03-26 08:45:38

标签: c# javascript session login

enter image description here

在我添加这部分会话代码之前,如果我的用户名和密码正确,它会将我重定向到BlogEntry.aspx。在我添加会话代码后,当我点击按钮时没有发生任何事情。你能帮我转换我的会话代码在javascript中工作吗?我需要在javascript中编码,而不是在后面的代码中,因为我使用的是html按钮。因为我想要按钮的设计,我需要这样做。除非你告诉我在后面的代码中有办法做到这一点。登录login.aspx后。它将重定向到logined.aspx并在标签中显示welcome admin。

的Login.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">

    </style>
    <link rel="stylesheet" type="text/css" href="stylesheets/loginstyle.css" />
    <script language="javascript" type="text/javascript">
// <![CDATA[




        function Button1_onclick() {

            if (document.getElementById('txtUserName').value == "Admin" && document.getElementById('txtPassword').value == "123") {

                //After add in session it cannot redirect    START HERE
                Session.Add("Username", txtUserName.Value);
                Session.Add("Password", txtPassword.Value);
                FormsAuthentication.SetAuthCookie(txtUserName.Value, true);                       
                window.location.assign("BlogEntry.aspx")
                //After add in session it cannot redirect    END HERE

            }
            else 
            {
                document.getElementById("<%=lblError.ClientID%>").innerHTML = "Name can't be blank!";
            }           
        }

// ]]>
    </script>
</head>
<body>

<div id="wrapper">

    <form name="login-form" class="login-form" action="" method="post">

        <div class="header">
        <h1>Login Form</h1>
        <span>Fill out the form below to login to my super awesome imaginary control panel.</span>
        </div>

        <div class="content">
        <input name="username" type="text" class="input username" placeholder="Username" runat="server" id="txtUserName" />
        <div class="user-icon"></div>
        <input name="password" type="password" class="input password" placeholder="Password" runat="server" id="txtPassword" />
        <div class="pass-icon"></div>       
        </div>

        <div class="footer">
        <input type="button" name="submit" value="Login" class="button" runat="server" id="Button1" önserverclick="Button1_Click"  onclick="return Button1_onclick()" />
            <asp:Label ID="lblError" runat="server" Text=""></asp:Label>
        </div>

    </form>

</div>
<div class="gradient"></div>
</body>
</html>

Logined.aspx

protected void Page_Load(object sender, EventArgs e)
{
    //Logout.Visible = false;
    string memName = (String)Session["UserName"];
    lblUsername.Text = String.Concat("Welcome Guest!");

    if (Session["Username"] != null && Session["Username"] != String.Empty)
    {
        lblUsername.Text = "Welcome, " + memName + "!";


    }
}

@Sangram

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用html会话存储或localStorage来存储您的键值。这不需要任何服务器端代码。即使用户关闭浏览器,信息也会存在。

用于浏览器兼容性:http://www.html5rocks.com/en/features/storage

例如:http://coding.smashingmagazine.com/2010/10/11/local-storage-and-how-to-use-it/