Click here to Skip to main content
15,889,556 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
I need to call a http handler using JQuery in my aspx page on button click. The handler must execute a method when it is called.pls help me how to do this. give me some sample about this. i am new to http handlers and Jquery.pls help me..
The handler must actually generate a pdf report for which i have written the fuction. i jus need to know how and where o call this method using jquery and http hander.pls help me ...
Posted

1 solution

In this example we have a sample generic handler that reads a text file and resturns its content. In your case it would generate and return pdf file

The Handler:

XML
Imports System.Web
Imports System.Web.Services
Imports System.Web.Script.Serialization
Imports System.Web.Caching
Imports System.IO
Imports System.Diagnostics

Public Class genHandler
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim testData As String = ""
        Dim serializer As New JavaScriptSerializer()
        Dim sbJsonResult As New StringBuilder()

        testData = getTestData(context)
        serializer.Serialize(testData, sbJsonResult)

        context.Response.Clear()
        context.Response.ContentType = "application/json; charset=utf-8"
        context.Response.Write(sbJsonResult.ToString())

    End Sub

    ''' <summary>
    ''' read text file and encode it as html
    ''' </summary>
    ''' <param name="context">HttpContext, HTTP specific information of the current request</param>
    ''' <returns>HTTP encoded string from a test text file</returns>
    ''' <remarks></remarks>
    Private Function getTestData(ByVal context As HttpContext) As String
        Dim data As String = ""
        Dim rootPath As String = context.Server.MapPath("~")

        Debug.WriteLine(String.Format("{0}{1}{2}", vbCrLf, "***", vbCrLf))
        Debug.WriteLine(String.Format("{0}", rootPath))

        If context.Request.QueryString("search") = 1 Then
            Dim sr As New StreamReader(Path.Combine(rootPath, "/TestData/1.txt"))
            data = sr.ReadToEnd()
            data = HttpUtility.HtmlEncode(data)
        ElseIf context.Request.QueryString("fileName") = 1 Then
            Dim sr As New StreamReader(Path.Combine(rootPath, "/TestData/2.txt"))
            data = sr.ReadToEnd()
            data = HttpUtility.HtmlEncode(data)
        Else
            data = "file not found on server."
        End If

        Return data
    End Function


    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class




Default.aspx page
XML
<html>
<head>
    <title></title>
    <link href="style/style.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/Script.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <select id="selectOption">
        <option value="1">File One</option>
        <option value="2">File Two</option>
    </select>
        <input type="submit" value="Get Data" onclick="javascript:loadDynamicContent();" />
        <div id="divLoading">
            Loading...
        </div>
        <div id="divResult">
        </div>
    </div>
    </form>
</body>
</html>



Script.js file
function loadDynamicContent() {    
    scroll(0, 0);
    showProgress(); //show the progress indicator
    var fileName = $('#selectOption').val();
    alert(fileName);
    $.getJSON(
        'BLL/genHandler.ashx?fileName' + fileName,
        function (data) {
            hideProgress(); //hide the progress indicator
            $("#divResult").css("display", "block");alert(data);
            $("#divResult").slideDown("slow"); 
            $("#divResult").html(data.d);
            alert(data);
        }
    );
}
function showProgress() {
    $("#divLoading").css("display", "block");
}
function hideProgress() {
    $("#divLoading").css("display", "none");
}


NOTE: Make sure you update the script and jquery path when you test the code above.

Use VB to C#[^] online conversion tool if you like to have it in C#

Hope this helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900