Content

  • Cookies in ASP.NET
  • Query String in ASP.NET
  • AutoPostBack property of web control
  • ASP.NET Button Event : OnClick
  • ASP.NET TextBox Event : OnTextChanged

Cookies in ASP.NET

Cookies can be used to send data from one webform to another. In general, web sites use cookies to store user preferences or other information that is client-specific. Cookies store small amounts of information on the client’s machine.

Types of Cookies

  • Persistent Cookies : Remain on the client computer, even after the browser is closed. You can configure how long the cookies remain using the expires property of the HttpCookie object. (But user can delete the cookies from browser tools)
  • Non-Persistent cookies : If you don't set the Expires property, then the cookie is called as a Non-Persistent cookie. Non-Persistent cookies only remain in memory until the browser is closed.
Example :
Frond-End code
//File 1
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Cookie Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <h3>Cookie Example</h3>
            <label>Name :</label><br />
            <asp:TextBox runat="server" ID="txtname"></asp:TextBox><br />
              <label>Email :</label><br />
             <asp:TextBox runat="server" ID="txtmail"></asp:TextBox><br /><br />
            <asp:Button runat="server" ID="btnsubmit" OnClick="btnsubmit_Click" Text="Go to other webform" />
        </div>
        </form>
    </body>
    </html>

    //File2
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Cookie Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <h3>Cookie Example</h3>
            <label>Name :</label> <asp:Label> runat="server" ID="lblname"></asp:Label><br />
            <label>Email :</label> <asp:Label> runat="server" ID="lblemail"></asp:Label>
        </div>
        </form>
    </body>
    </html>
Back-End code
//File1 code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = new HttpCookie("UserInfo");
            cookie["Name"] = txtname.Text;
            cookie["Email"] = txtmail.Text;
            //To sepcify expiry of cookie
            cookie.Expires = DateTime.Now.AddDays(30);
            Response.Cookies.Add(cookie);
            Response.Redirect("CookieEx2.aspx");

        }
    }
}

//File 2 Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Training
{
    public partial class CookieEx2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["UserInfo"];
            if(cookie != null)
            {
                lblname.Text = cookie["Name"];
                lblemail.Text = cookie["Email"];
            }
            
        }
    }
}

Query String in ASP.NET

  • Querystrings are name/value collection pairs
  • Using querystrings, is a very comman way to send data from one webform to another.
  • Query strings are appended to the page URL.
  • In page URL, ?(Question Mark), indicates the beginning of a query string and it's value.
  • It is possible to use more than one query string. The first query string is specified using the ?(question mark). Subsequent query strings can be appended to the URL using the &(ampersand) symbol.
  • Query strings are visible to the user, hence should not be used to send sensitive information, unless encrypted.
  • To read the query string value, use Request object's QueryString property.
Example :
Frond-End code
//File1
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Query String Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <h3>Query String Example</h3>
            <label>Name :</label><br />
            <asp:TextBox runat="server" ID="txtname"></asp:TextBox><br />
              <label>Email :</label><br />
             <asp:TextBox runat="server" ID="txtmail"></asp:TextBox><br /><br />
            <asp:Button runat="server" ID="btnsubmit" OnClick="btnsubmit_Click" Text="Go to other webform" />
        </div>
        
        </form>
    </body>
    </html>

    //File2
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Query String Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <h3>Query String Example</h3>
        <label>Name :</label> <asp:Label> runat="server" ID="lblname"></asp:Label><br />
        <label>Email :</label> <asp:Label> runat="server" ID="lblemail"></asp:Label>
    </div>
    </form>
</body>
</html>
Back-End code
//File 1 Code Behind
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Training
    {
        public partial class QueryString1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void btnsubmit_Click(object sender, EventArgs e)
            {
                Response.Redirect("QueryString2.aspx?Name="+txtname.Text+"&Email="+txtmail.Text+"");
            }
        }
    }

//File2 Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Training
{
    public partial class QueryString2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblname.Text = Request.QueryString["Name"];
            lblemail.Text = Request.QueryString["Email"];
        }
    }
}

AutoPostBack property of web control

  • Autopostback is the mechanism by which the page will be posted back to the server automatically based on some events in the web controls.
  • Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the web control is the most common way to change focus.

ASP.NET Button Event : OnClick

  • Button OnClick() method raises the click event of the button control.
  • Button Click event occurs when the button control is clicked

ASP.NET TextBox Event : OnTextChanged

  • The TextBox TextChanged event occurs when text changed in textbox control
  • We have to set AutoPostBack=True for OnTextChanged event

Example :
Frond-End code
//Front-end code
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Text Changed Event Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <h3>Text Changed Event Example</h3>
            <label>Enter Name :</label><br />
            <asp:TextBox runat="server" ID="txtname" OnTextChanged="txtname_TextChanged" AutoPostBack="true"></asp:TextBox><br />
            <label>You have entered :</label><br />
            <asp:Label runat="server" ID="lblname"></asp:Label>
        </div>
        </form>
    </body>
    </html>
Back-End code
using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Training
    {
        public partial class TextChangedEx : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void txtname_TextChanged(object sender, EventArgs e)
            {
                lblname.Text = txtname.Text;
                txtname.Text = "";
            }
        }
    }