Showing posts with label server request by client side without postback. Show all posts
Showing posts with label server request by client side without postback. Show all posts

Monday, July 9, 2012

ASP.NET(Send Server Request Using Javascript)


//This Example Will Allow You To Send Request On The Server Using Javascript

//Step01:JS code will help you to send the request on HTTP Handler.


var var01 = document.getElementById(elementID1);var var02 = document.getElementById(elementID2);
var var03 = document.getElementById( elementID3);

var dataString = ' var01=' + var01 + '&var02=' + var02 + '&var03=' + var03;

$.ajax({
                type: "POST",
                url: "HandlerName.ashx",
                data: dataString
            })
            .done(function (msg) { alert("Done: " + msg);})
            .fail(function (msg) { alert("NotDone: " + msg); })
            .always(function () { alert("Complete"); });

//Step02:Handler Performing the Insert Query Into DataBase.(You can also Update, delete the record into the database or etc…)
public class manageaddress : IHttpHandler, IRequiresSessionState {

public
 void ProcessRequest (HttpContext context)
    {
        context.Response.ContentType = "text/html";
        try
        {

        if (context.Request.Form["var01"] != null && context.Request.Form["var02"] != null &&           context.Request.Form["var03"] != null)
        {
          
                SqlConnection cn = new SqlConnection();
                cn.ConnectionString =ConfigurationManager.ConnectionStrings["ConnectinStringName"].ConnectionString;
                cn.Open();

                string strSql = "INSERT INTO <Table_Name> (column01, column02, column03) values (@var01, @var02, @var03)";
               
 SqlCommand command = new SqlCommand(strSql, cn);
                command.Parameters.AddWithValue("@var01", context.Request.Form["var01"].ToString());
                command.Parameters.AddWithValue("@var02", context.Request.Form["var02"].ToString());
                command.Parameters.AddWithValue("@var03", context.Request.Form["var03"].ToString());

                command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}