Tuesday, December 27, 2011

Cookies

<head runat="server">
    <title></title>
   
    <script type="text/javascript" language="javascript">
        function __doPostBack(eventTarget, eventArgument) {
            if (!form1.onsubmit || (theForm.onsubmit() != false)) {
                form1.__EVENTTARGET.value = eventTarget;
                form1.__EVENTARGUMENT.value = eventArgument;
                form1.submit();
            }
        }
</script>

     <script type="text/javascript" language="javascript">

         function readCookie(cookies) {
             var txtuname = document.getElementById('<%=t1.ClientID %>')
             var txtpass = document.getElementById('<%=t2.ClientID %>')
             if (document.cookie.length > 0 && txtuname.value.length > 0) {
                 // check the index of the username that is entered by user in Username text box
                 var c_start = document.cookie.indexOf(txtuname.value);
                 if (c_start != -1) {
                     var c_end = document.cookie.indexOf(";", c_start);
                     if (c_end == -1) {
                         c_end = document.cookie.length;
                     }
                     var value = unescape(document.cookie.substring(c_start, c_end));
                     var arr = new Array(10);
                     arr = value.split('=');
                     // if only 0ne equal sign is present that is default one
                     if (arr[0] == txtuname.value && arr.length == 2) {
                         txtpass.value = arr[1];
                     }
                     // if the password contain =(equal) signs in it
                     else if (arr[0] == txtuname.value && arr.length > 2) {
                         var pass = arr[1];
                         for (i = 2; i < arr.length; i++) {
                             pass += arr[i];
                         }
                         txtpass.value = pass;
                     }
                     else {
                         txtpass.value = "";
                     }
                 }
                 else {
                     txtpass.value = "";
                 }
             }
             else {
                 txtpass.value = "";
             }
         }


         function CheckValidation()
             
              {
             var ReturnValue = true;
             var lblMessage = document.getElementById('<%=lblMessage.ClientID %>');
             var txtUserName = document.getElementById("<%=t1.ClientID%>");
             var txtPassword = document.getElementById('<%=t2.ClientID %>');
             if (trim(txtUserName.value).length <= 0) {
                 lblMessage.innerHTML = "<div class='RedMessage' style='padding:3px 3px 3px 3px;'>Please enter the User ID.</div>";
                 txtUserName.focus();
                 ReturnValue = false;
             }
             else if (trim(txtUserName.value).length > 0)
             {
                 var CharPat = /^[a-zA-Z0-9\-_@.]+$/;
                 var matchArray = trim(txtUserName.value).match(CharPat);
                 if (matchArray == null)
                 {
                     lblMessage.innerHTML = "<div class='RedMessage' style='padding:3px 3px 3px 3px;'>Please enter the valid characters in User ID.</div>";
                     txtUserName.focus();
                     txtUserName.select();
                     ReturnValue = false;
                 }
                 else
                 {
                     if (trim(txtPassword.value).length <= 0) {
                         lblMessage.innerHTML = "<div class='RedMessage' style='padding:3px 3px 3px 3px;'>Please enter the Password.</div>";
                         txtPassword.focus();
                         ReturnValue = false;
                     }
                 }
             }

             if (ReturnValue == true) {
                 lblMessage.innerHTML = "";
                 ReturnValue = false;
               __doPostBack('<%=btn.UniqueID %>', '');
             }
             return ReturnValue;
         }


         function trim(s) {
             return s.replace(/^\s+|\s+$/, '');
         }
       
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
   
  
    <div>
   
    <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
    <table>
    <tr>
    <td>
    <asp:TextBox ID="t1" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:TextBox ID="t2" runat="server">
    </asp:TextBox>
      </td>
    </tr>
    <tr>

  <td align="left">
   <asp:CheckBox ID="chkRemember" runat="server" Text="Remember password" />
  </td>
    </tr>
   
    <tr><td><asp:Button ID="btn" Text="Click" runat="server"
            OnClientClick="return CheckValidation();" onclick="btn_Click" /></td></tr>
   
    <tr>
                                            <td colspan="2" align="center" class="CommonFont">
                                                <asp:Label ID="lblMessage" runat="server"></asp:Label>
                                            </td>
                                        </tr>
    </table>
    </div>
    </form>
</body>





protected void Page_Load(object sender, EventArgs e)
        {

            string JavaScript = "javascript:if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)){return CheckValidation();}else {return true;}";
            t1.Attributes.Add("onkeydown", JavaScript);
            t2.Attributes.Add("onkeydown", JavaScript);
            chkRemember.Attributes.Add("onkeydown", JavaScript);
            t2.Attributes.Add("OnFocus", "javascript:readCookie('" + Page.Request.Cookies.ToString() + "');");

           
        }

 
        protected void btn_Click(object sender, EventArgs e)
        {

            if (chkRemember.Checked)
            {
                HttpCookie cookie = new HttpCookie(t1.Text.ToString());
                Response.Cookies.Add(cookie);
                cookie.Values.Add("", t2.Text.ToString() + ";");
                Response.Cookies[t2.Text.ToString()].Expires = DateTime.Now.AddDays(15);
            }

                   


           Response.Redirect("WebForm2.aspx");
        }

No comments:

Post a Comment