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?
Related
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
I am new at C#.Net and i have one question that i couldn't manage to find on the internet. When should i use a classic combination of html + javascript + css instead of using an aspx page with a code behind?
As i experienced since i started .net , i found that aspx pages and code behind is a huge ease for developers. I didnt need any piece of javascript code since i started. There must be something wrong. I think i am missing a point. Can you answer my question and tell me some examples that i must use html+javascript+css instead of aspx + aspx.cs or vice versa??
Have a nice day.
Javascript is a client side technology, running only in the browser, whereas ASP.NET runs on the server side. These allow you to achieve different and complementary things.
With a classic server side language, any user interaction that you want to respond to must typically be posted across the internet from the browser to your server. It is then processed by the server, which responds with a new page for the browser to load. This generally means that the response time for the user is slower, though you will have access to a much richer programming environment on the server.
With a client side language, everything is processed on the browser. This allows for faster feedback to the user, though at the expense of working within the much more restricted programming environment that the browser gives you, and with no access to stuff your application may depend on, such as your database.
Of course, the lines are blurred somewhat when you make an AJAX request (usually a call written in Javascript that makes a request to the server, receives the response, and updates the page dynamically).
You mention that you have not used any Javascript so far. Perhaps as a starting point you'd like to investigate validating user input on the client side? This way, errors are caught and reported to the user immediately without the cost of the round trip to the server. http://www.tizag.com/javascriptT/javascriptform.php
Both client side and server side technologies can be powerful and useful. Use a combination of them both to give the best experience for the user.
In my experience, using Javascript/jQuery in .NET has been for UI and client-side validation purposes. If you are building an app that does not require Javascript to meet your client's requirements, then take advantage of what .NET has to offer. However, implementing Javascript is not that hard, so feel free to use what you prefer and is in the best interest of the client. You can still write and use Javascript in an ASPX page.
One of your considerations might well be speed. Javascript in a web-page will run on a site visitor's browser. Code-behind runs on the server hosting the page.
from my experience the main aim to use the companion of html,css,javascript with asp.net when the client needs is for a web app that acts exactly like win app
that u don't need to flush over the page to the server and come back again
Points you are missing
Code behind is not what makes ASP.NET; you can make a web app with all C# code right into the aspx files.
If you choose ASP.NET and C# for a web app, all your pages should be aspx ones, except for very specific and not very common situations.
You need to understand the difference between server side scripting and client side scripting. ASP.NET is a server side scripting technology while javascript is a client side only one. Take a look.
You can create aspx pages that are as simple as you want -even without any relevant serverside scripting- and thats all right.
Attempt to answer
You use the word must, so:
You must use aspx instead of only javascript if you want any kind of server side processing.
You must use an html file with plain javascript, jquery and css if there is the specific requeriment to do so, wich would be very uncommon. That could be a situation where a) the page should be as fast as it can possible be b) you don't mind everyone to be able to see your full code by just selecting view source on the browser c) there is no need at all for server side processing d) you don't mind about the little extra mix of technologies on your web application.
You use Javascript/JQuery to perform operations that does not need any server side processing like validating controls for a range or for empty values, some fancy UI stuff. It is much faster than the code behind because it does not post back to server however you could use UpdatePanel aspx server control to have partial post back and avoid reloading the page.
As a web developer you should always use combination of server-side processing and client-side processing. Working logic and application processes on the client-side allows browser based applications to seem more responsive and to have more "snappiness" to them.
If you are looking for highly customizable and high performance pages, then I would go with html + javascript + css and make calls to some webservice. This way you are not restricted by asp.net controls. Also, there are a lot of caveats with standard out of the box web forms that can lead to page performance issues and overhead - ViewState being one. You can also design your own asp.net controls as well, but there is some learning curve.
It really boils down to personal preference (there isn't anything in one that you can't do in the other) : Fundamentals vs Abstraction. To me javascript has always felt somewhat cumbersome when used in conjunction with webforms, however, with mvc it is a lot more natural, as it would be with a standard html + javascript + css page.
When you want to create static pages you can use html+css+javascript instead aspx.
In case you want things more dynamic you have to use aspx with cs.
For more info go http://www.w3schools.com/aspnet/aspnet_pages.asp
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(...
The .NET WebClient class is nice and all, but it doesn't parse the JavaScript in the page.
What I mean is, sometimes a page is forwarded to a different page via JavaScript, or sometimes DOM elements get added to the page via JavaScript with an onload event. I need to deal with both of these situations.
What can I do to deal with this? I'm using WPF if that matters.
To do this in managed code would essentially mean writing your own browser. I expect that to do what you want the simplest approach would be to drop a WebBrowser control into the page (essentially re-hosting IE) and use that. There are other hostable controls, of course - but that is the simplest for Windows.
You can use the WebBrowser control. This approach is not as "lightweight" as using the WebClient, but short of writing your own javascript parser this is probably the easiest solution.
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!