AaA
AaA

Reputation: 3694

Dynamic data for JQuery in ASP.NET pages

Problem

I have some pages that need dynamic data from website to generate output to users.

A simple solution is an aspx(php, ...) page to generate data and create another html page serving as GUI retrieving data from first page and showing it to users. in this method I can call my GUI page for example form1.aspx and my data page form1.json.aspx. although I personally like this method, it is not suitable when creating components for it.

Another method that currently I'm using is using same GUI page call itself with a querystring to retrieve data. this page should check for that query string and if it exists, only generate data and remove everything else from page. As an example for this method if I call my page form1.aspx, to retrieve data, I need to call it like form1.aspx?JSON

Here is an example of what I'm doing:

protected void Page_Load(object sender, EventArgs e) {
    if (Request.QueryString.ToString().IndexOf("JSON") == 0){
        this.Controls.Clear();
        Response.Clear();
        // send pure data to client
    } else {
      // render page as GUI
    }
}

However this method becomes too messy if I add master page and/or inherit my page from some template page. Master pages can only removed in Page_PreInit and that adds another extra method.

Security controls cause another problem, if user leaves page open for long time until session expires any attempt to retrieve data will fail cause security module will redirect the request to login page.

Next problem is I cannot consolidate my component in package because it needs modification in page (removing master page, clearing page components ...).

What I'm looking for:

1- I'm looking for a solution that I can call my page and get pure data (JSON or XML format) and doing so run a server side method that generates data, so I don't have to worry about what another designer puts in their master page or template.

2- I think it is possible to use axd extension to do this but I don't have a clue about it and couldn't find a helping document either.

3- Is there any better way of doing this. any suggestion or solution to improve this much appreciated.

Upvotes: 1

Views: 640

Answers (2)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Other ways of doing is using an HTTP Handler. Implement IHttpHandler interface and register your implementation in your Web.config file. Later call it using jQuery ($.get / $.post):

EDIT As OP pointed out, in order to access session state in a page method you should use WebMethodAttribute this way:

[WebMethod(EnableSession = true)]

Upvotes: 2

Agung Gugiaji
Agung Gugiaji

Reputation: 121

I think you can use webservice instead of aspx page to return a JSON or XML string and then the caller page (any aspx page) will response after process is success.

So with this webservice, any third party page will have access to your server side method.

To create a webservice pls Check this link: Create and use Asp.net web service basic

Regards

Upvotes: 0

Related Questions