Use a webform page to handle an ajax post.

June 13, 2014 by: B.Harding

Today I needed to make an ajax call in an older webform site. Something easy¬†for mvc or maybe webapi but that’s overkill.

I decided to make a web form to do the work.

First I need to tap in to the event.

When I make the call from javascript using $.ajax() I can handle the request by putting this in page_load. Note they are separate pages in my solution..

 

if (!IsPostBack && Request.HttpMethod == “POST”)

it IS a post but not a postback.

to send a message back I do this

Response.Write(message);
Response.End();

this should send back just your message and not any sort of html.

 

Make sense? So there you have it. a quick and dirty way to do things requested by ajax using a webform..


protected void Page_Load(object sender, EventArgs e)
{
//act like a rest call for ajax posts from the footer.
if (!IsPostBack && Request.HttpMethod == "POST")
{
var email = Request.Form["EmailAddress"];
var message = string.Empty;
if (!string.IsNullOrEmpty(email) )
{
//process the request

message = "Thank you for Signing Up";

}
else
{
message = "Error: Not Found";
}

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

Filed under: Uncategorized

Leave a Reply

*