I am currently working on a ASP.NET AJAX application. Having decided to not use UpdatePanels for evident reasons, what alternatives to I have? This application has pages built dynamically and so most or all the components of the page exist as User Controls.
I need to make AJAX calls from ASCX user control page. And as ASCX user controls may not contain Page Methods, what other options do I have?
Or, is there a way to get around using Page Methods in ASCX page?
You can't call webmethods from a UserControl as you said. The reason for this is methods marked with the WebMethod attribute must also be static. UserControls don't support this. If you aren't willing to use an UpdatePanel, you don't have a lot of options.
You could make AJAX calls via jQuery to web services? This would be one option...
UpdatePanel controls used with UpdateMode="Conditional" gives you fairly good control over what is happening with the callbacks and is still a reasonable solution. It won't be as lightweight as a straight ajax call but will be easy to maintain, etc.
jQuery AJAX is my recommendation, used with web services provides a nice solution.
Best way (that I discovered) is to
1. Place your PageMethods on the parent page
2. When the PageMethod completes, let your user control know via a JS method that it can the PageMethod call has been completed
3. Then a ICallbackHandler can be implemented on the user control to do a AJAX callback on the user control's method
In my case, I need to update some values on the user control after the PageMethods gets executed, so I build my user control using HtmlOutputWriter to update the contents.
Works for me!
Related
I have an asp.net page which runs many functions in the page_load. Thus making the page so slowly. I want the page to be shown to the user first and then run the functions inside the page load. Is that anyway I can show the page first and then run the code inside the page_load?
First I suggest you move to MVC and don't use webforms which is so old.
In both ways you can split the HTML markup + Javascript code without using webforms controllers, Then simply using the javascript onPageLoad event and there fire the ajax reqeusts to the server..
If you really really want to use the controllers of webforms you can see the page lifecycle here: https://msdn.microsoft.com/en-us/library/system.web.ui.page_events(v=vs.110).aspx then you can use for instance the PreRender event.
I have control1 which makes an ajax call and depending on what is returned I may need to call a javascript function on control2.
Both live within the same page.
Is there a way to do this?
I'm using C#
Yes, it's possible. JavaScript lives on the client end and doesn't really care how it gets composited into the final page from the server side.
However, are you sure that this is a good architecture? Shouldn't your ASCX user controls be self contained?
I'm in a huge bind.
Long story short, I am working on a big project and am learning ASP.NET, C#, everything as I go.
The elementals of my project are comprised of user controls. My line of thinking was that I could create many user controls, each performing a function for a "component" of the project I'm building. Up until now I have been using clientside scripting to postback ajax calls to the code-behind on each of my user controls. AJAX worked well because it allowed to me pass data(that I need from the client) to my user controls and then I could return something in order to do an action.
I have been using a method for generating querystrings to create a callback "action" in order to determine what method needs to handle what data when the postback is sent to the code-behind side.
My problem now is that I need to start using many user controls one page -- and so now whenever ANY control does a postback ALL of the controls go through a page load. I thought my callback solution would take of this, but it isn't. Particularly when I drop a custom registered control into another user control.
I have done multitudes of research and having seen various ways to get around this, the best of them being [WebMethod] and controllers. However the project I am working on is NON-MVC.
I am also attempting to use UpdatePanel controls to minimize postback to the entire page but have had little success.
What can I use as alternatives? I feel like I'm running out of options or am missing something very basic here.
TL;DR -- I need a non-MVC method to pass data to user controls that can distinguish between multiple controls. Cannot use pagemethods(or page). Manual ajax calls are not working out. Cannot afford to do a full postback
Take a look at:
updatepanel vs page methods
Based on this:
My problem now is that I need to start using many user controls one page -- and so now whenever ANY control does a postback ALL of the controls go through a page load.
This might sound simple but have you tried to use if(!this.IsPostBack) in your load events?
Well not, the only way to avoid this situation, is using PageMethods or create a Script Service to handle AJAX requests (Web services with the ScriptService attribute or WCF REST services)
Note that even if you use the evil UpdatePanel, absolutely all the page life cycle will execute, which means that the whole page viewstate has to be sent in each post, the only advantage of using UpdatePanel controls is that you gain partial rendering, that's it, the performance on the server side doesn't change at all.
So you could use PageMethods or Script Services. But there's a catch, if you start using them you will notice an incredible performance change, your application will be more responsive (RIA applications), but the catch is that you won't be able to use the benefits of the ASP.Net server controls such as GridView, Repeater, etc. In other words you would need to change most of your view controls (this is the approach followed when working with MVC applications)
You can create static methods on your aspx page and mark it with [WebMethod]. Then you can call the method using jQuery ajax from the user user control markup. Take a look at this blog
In chat scripts new lines adds without reloading page. User's availability status changes without postback. Even in facebook a new comment adds without reloading page. How to fire an event in ASP.NET using C# so that event takes place with reloading the page.
I believe that its not possible using C#. Is there any special library for it in AJAX or jQuery or other?
It's done using native XML components available from the browser when writing JavaScript. These are used to call a server-side page or service to load a block of HTML (or XML) async, and then write it into the document. This is essentially AJAX.
JQuery is a library designed to make writing cross browser compatible scripts easier, and offers built in functionality to help make those AJAX calls for you, without you needing to do the low level bits yourself.
ASP.NET has a range of tools for creating AJAX enabled sites, and recent versions now incorporate jQuery for the client side parts, without you needed to do much wiring up. Together the tools make exposing and calling ASP.NET services really easy. The simplest form is probably the UpdatePanel. It's a good place to start if you have never made an AJAX enabled site before.
Also take a look at the AJAX Toolkit.
jQuery itself allows you to send GET and POST Ajax requests and manipulate with HTML elements
$.post(...
$.get(...
Is there a simple way to determine if there is a control on a page that requires a scriptmanager? I am adding a scriptmanager to every page (which works great) in my page init, but it seems like there would be a better way than just forcing every page to have a scriptmanager. Maybe 1 in 4 pages have update panels, or some sort of extender. Or is it not much of an extra load and just not worry about it?
Use inheritance to your advantage. Create some sort of ScriptManagerPageBase class whose PageInit method invokes a new ScriptManager. For every page that requires your ScriptManager, such as using UpdatePanel control, simply have that page inherit from ScriptManagerPageBase and you'll get that functionality only when you need it, without having to duplicate code.
In your master page (or a base page depending on your approach, I recommend master pages..) just add a scriptmanager. Unless needed it will not affect the page at runtime. Then add a scriptmanagerproxy to each page or control that needs a scriptmanager. At runtime the proxy will find the base or master manager and register any previously unregistered scripts. See MSDN SCRIPTMANAGERPROXY discription