Question

I have gone though lot of posts regarding AJAX in ASP .Net, but I am still not convinced on using updatepanel. Even when we use updatepanel, the system will go through all the life cycle of an element.

For example, a simple add two numbers and display it in a textbox has to go through the page life cycles. I do not know whether my understand is right.

We can use web methods or could use web services etc., but the problem is we will not be able to access elements/controls. I could use the JQuery, but I end up with the same issue of not able to access elements/controls.

Please help me as to whether my understandings are correct or not.. Thanks in advance.

Was it helpful?

Solution

Update Panel is one of the most simplest way of implementing AJAX on your site. On your local system, you may tend to drop half a dozen Update Panel and it will work smoothly.

However, the same Update Panel will go through the entire page life cycle and may create performance bottlenecks in live environment. The use of update panel will also increase the overall page size.

Page Methods are far better as instead of posting back and then receiving HTML markup to completely replace our UpdatePanel’s contents, we can use a method to request only the information that we’re interested in.

Answering to your questions:

Understanding is correct?

Your understanding of ASP.NET AJAX/JQuery is completely correct.

Issue in accessing controls in code behind files?

Only ASP.NET ajax will allow you to access the controls in code behind files. However that comes with a performance bottleneck. Once you get used to other Javascript frameworks such as JQuery, you will always find a workaround of resolving this issue.

My suggestion is to use ASP.NET AJAX on site where performance requirements are not too high. You can also use it on intranet application.

But when it comes to performance, use a light-weight javascript library.

OTHER TIPS

Yes. In web method or web service you cannot access web controls. But still if you want to assign values to the control you can return the value and assign to the control.

Lets take an example of adding two values.

In textbox1 you insert 5 and in textbox2 you insert 6.

Now on click event of the add button call the web method through JQuery and pass the values of the textbox1 and textbox2 as an input parameter. and return the sum of those two values.

On OnSuccess event of Ajax call you can assign the result value to the label where you want to display the sum.

function GetAnswer() {
    $.ajax({
        type: "Post",
        url: "Default.aspx/GetAnswer",
        data: "{'value1':'5', 'value2' : '6'}",
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            alert(result.d);
        }
    });
}

And Web Method :

[WebMethod]
public static int GetAnswer(int value1, int value2)
{
    return value1 + value2;
}

You are talking really about two entirely different approaches. ASP.NET web forms (in which you use updatepanels) focuses on server-side processing. It tries to pretend that the web is a connected infrastructure, when really the client and server are highly disconnected. UpdatePanels just try to prevent 'blinking', but they aren't AJAX in its purest form (which focuses on only sending data across the wire, not HTML).

UpdatePanels just replaces big slices of HTML via AJAX rather than standard browser requests (i.e. the page refreshes and you get new HTML from the server). So you lose the benefit of AJAX, in that you are still transferring large amounts of text (the HTML rendered on the server).

On the other hand there are other architectures that Microsoft has been pushing for some time now. ASP.NET MVC is much more friendly to the 'AJAX with jQuery' approach. Also Single Page Applications (SPAs) are the current rage, and Microsoft has an approach for that via KnockoutJS. These approaches focus on rendering the HTML client side, with only data coming from the server. You have to have a much better understanding of how the web really works, but it pays off greatly in flexibility, performance, and user experience.

Ultimately if its important for you to have a 'desktop-like' experience (no page refreshes) then ASP.NET web forms is probably not the best choice for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top