Sunday, September 21, 2014

GET POST Method in ASP.NET C#

Many of you are aware of the GTE, POST, PUT & DELETE methods in ASP.NET, these are the basic method to send the value form one Form to another Form. Lets see how these work in ASP.NET project using C#.

GET :
Using GET method you can send all the input (textbox,textarea,radio button, checkbox etc..) to another form or another page. So how to do this?

Take a HTML page and write down the following HTML codes.

In Default.aspx to get the value of these two text boxes values we have to write code in Page_Load method. Make one thing clear, in the time of GET, POST we use NAME instead of ID. So lets check what will be for get method.
protected void Page_Load(object sender, EventArgs e)
{
     string name = ""; string pass = "";
     if (Request.QueryString["Name"] != null && Request.QueryString["Password"] != null)
     {
           name = Request.QueryString["Name"].ToString();
           pass = Request.QueryString["Password"].ToString();
     }
}

By Request.QueryString["Name"].ToString() this one we can get the value of a field which is sending through GET method. When we hit the submit button the URL will be something like this.

localhost/default.aspx?Name=arka&Password=arka1 

Normally we don't use GET to send sensitive fields like Passwords because it shows in the URL, I used it here just for an example.

POST :
Unlike GET in the POST method we can't see the values in the URL. It sends the values internally. Basically it has 2 parts. Head and body. In the head section it holds the variable name and in the body part holds the value. We will use the same example for the POST also.
Now lets check how to get the value using POST method in C# coding.
protected void Page_Load(object sender, EventArgs e)
{
     string name = ""; string pass = "";
     if (Request.Form["Name"] != null && Request.Form["Password"] != null)
     {
           name = Request.Form["Name"].ToString();
           pass = Request.Form["Password"].ToString();
     }
}
use Request.Form instead of Request.QueryString to get the value from POST method. And rest of the thing is same as GET.

0 comments:

Post a Comment

Popular Posts

Pageviews