Saturday 19 November 2016

.asp web form pass parameter by url, cookie



//default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="webform_pass_parameter._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table>
            <tr>
                <td style ="width:100px"><asp:Label ID="Label1" runat="server" Text="User ID:"></asp:Label></td>
                <td style ="width:200px"><asp:TextBox ID="ux_id" runat="server"></asp:TextBox></td>
            </tr>

//table rows follows first row style
            <tr>
                <td> Password:</td>
                <td><asp:TextBox ID="ux_password" runat="server"></asp:TextBox></td>
            </tr>

            <tr>
                <td colspan="2" style="text-align:center">
                    <asp:Button ID="Button2" runat="server" Text="submit" Width="87px" OnClick="Button2_Click" />
                    &nbsp;
                    <asp:Button ID="Button3" runat="server" Text="cookie" Width="83px" OnClick="Button3_Click" />
                </td>
            </tr>
        </table>      
   
    </div>
    </form>
</body>
</html>

------------------------------------------------------------------
//default.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace webform_pass_parameter
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string url = string.Format("~/process.aspx?userid={0}&password={1}",ux_id.Text, ux_password.Text);

            Response.Redirect(url);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            var cookie = new HttpCookie("userid", ux_id.Text);
            cookie.Expires = DateTime.Now.AddMinutes(1);
            Response.Cookies.Add(cookie);

            cookie = new HttpCookie("password", ux_password.Text);
            cookie.Expires = DateTime.Now.AddMinutes(1);
            Response.Cookies.Add(cookie);

            Response.Redirect("~/process.aspx");
        }
    }
}

----------------------------------------------------------------------
//process.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace webform_pass_parameter
{
    public partial class process : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string user_id = Request.QueryString["userid"];
            //string _password = Request.QueryString["password"];

            //if (user_id != "" && _password != "")
            //{
            //    string message = string.Format("<h3>wecome {0}, your password is {1}</h3>", user_id, _password);

            //    Response.Write(message);
            //}

            if (Request.Cookies["userid"] != null && Request.Cookies["password"]!=null)
            {
                string user_id = Request.Cookies["userid"].Value;
                string password = Request.Cookies["password"].Value;
                string message = string.Format("<h3>welcome {0}, your password is {1} </h3>",user_id,password);

                Response.Write(message);
            }
        }
    }
}

No comments:

Post a Comment