Share via


Using method type post/get in asp.net web form

 

I  will start the article from with detailed information.

Actually form has two types of in asp.net2.0.

  1. Get
  2. Post

When working with Get method:

  • We can access all form input variables in the next page which we mentioned in the action attribute.
  • All the submitted information is displayed in the address bar as part of the URL.
  •  Url Which is not secured because values will be shown in address bar

When working with Post method:

  • we can access the variables in the page which we mentioned in the action attribute.
  • we can access those variable as shown below
  • which is more secured, variable not accessible

Now we will have small application with 2 web pages

1)       default.aspx

2)       Webform.aspx

GET:

  • I have given the value for action attribute is webform1.aspx in the default.aspx page with method type

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

   <form action="webform1.aspx" method="get" >

   First name: <input type="text" name="fname" /><br />

   Last name: <input type="text" name="lname" /><br />

   Age: <input type="text" name="age" /><br />

   <input type="submit" value="Submit" />

 </form>

</body>

</html>

  • Variables will be available in the address bar like below .

http://localhost:50920/webform1.aspx?fname=jhon&lname=smith&age=30

 

  • We can access the variables from the Address to Form using Request.QueryString[] like below.

  protected void Page_Load(object sender, EventArgs e)

        {

            if (Request.QueryString["fname"] != null)

            {

                Response.Write("fname : " + Request.QueryString["fname"] + \n");

            }

            if (Request.QueryString["lname"] != null)

            {

                Response.Write("lname : " + Request.QueryString["lname"] + "");

            }

       }

POST:

  • Variables will be post to the next page  using Post method type

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

   <form action="webform1.aspx" method="post" >

   First name: <input type="text" name="fname" /><br />

   Last name: <input type="text" name="lname" /><br />

   Age: <input type="text" name="age" /><br />

   <input type="submit" value="Submit" />

 </form>

</body>

</html>

  • We cannot access the variables from the url.

http://localhost:50920/webform1.aspx

 

  • We can access the variables from the request. Form [].

  protected void Page_Load(object sender, EventArgs e)

        {

            if (Request.Form["fname"] != null)

            {

                Response.Write("fname : " + Request.Form["fname"] + "\n");

            }

            if (Request.Form["lname"] != null)

            {

                Response.Write("lname : " + Request.Form["lname"] + "");

            }

        }

 

See Also