Monday, April 16, 2012

Form submission using Post Method -by ASP.Net Handler !

The topic describes how to post a form using ASP.Net generic handlers.


public class reqTpNb : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {



 string formToSubmit = fnBuildForm("http://ahsaan-ansari@blogspot.com", strMsg);
                context.Response.Write(formToSubmit);

}

public string fnBuildForm(string url,string data)
    {
        string formID = "PostForm";
        //Build the form using the specified data to be posted.
        System.Text.StringBuilder strForm = new System.Text.StringBuilder();
        strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\"            method=\"POST\">");


        strForm.Append("<input type=\"hidden\" name=\"msg\" value=\"" + data + "\">"); 
        strForm.Append("</form>");
        
        //Build the JavaScript which will do the Posting operation.
        System.Text.StringBuilder strScript = new System.Text.StringBuilder();
        strScript.Append("<script language='javascript'>");
        strScript.Append("var v" + formID + " = document." + formID + ";");
        strScript.Append("v" + formID + ".method='post';");
        strScript.Append("v" + formID + ".action='" + url + "';");
        strScript.Append("v" + formID + ".submit();");
        strScript.Append("</script>");
        //Return the form and the script concatenated.
        //(The order is important, Form then JavaScript)
        return strForm.ToString() + strScript.ToString();         
    }


Enjoy happy coding....



2 comments: