I know the method that, from jQuery, using ajax I can invoke WebMethod from a aspx or asmx file. That's ok, but I only can place my project logic in ascx.cs files. It is a specific CMS and I can't do anything about it.
So my question is based on example described below:
Lets say users is logged in and is viewing an article. One user wants to mark it as a favourite, so clicks a button. On the server side without, refreshing the page, an appropriate method should run which adds this article to his favourites and then in client side there is an alert - 'Success'.
I dont want anyone to write the code for me for that it is just an example for desribing what functionality I would like to be able to achieve and which technology to use for that.
Thanks for the help.
P.S. I'm using ASP.Net 2.0
ASCX files are not directly accessible from the client (and as such, cannot be targeted via AJAX calls).
If your logic really has to be encapsulated in the ASCX file, you can add in an entrypoint WebMethod in your ASPX that calls the respective ASCX method instead. You'll probably encounter some difficulties related to the WebMethods being static though, so you may eventually need to refactor a bit, depending on how your code is structured now.
You can make ajax call to remote page (with ascx control with your server side method) and then parse output (for example look for world "SUCCESS") to verify, that your method is executed. Not very elegant, but it will work.
Related
I have seen 100 examples of posting from a HTML form to a .aspx page and using Request.Form to see the values; this makes sense.
I'm trying to build up a small example to mimic a project where data is posted to a blank .aspx page and the values just read server side. I know it seems odd to leverage a .aspx page for this purpose, but that's the objective.
I want to know which event will be raised on the server in my Default.aspx page when data is POSTed to it? I doub't Page_Load() fires because the page is not being pyhsically opened, rather just hosted in IIS on the server.
Which event will I use in Default.aspx to read or siphon out the POSTed data?
EDIT: 'mimic' is the keyword here. This is not a new project, but I don't have the source for the original - it's a prototype to mimic an implemented example. If I was starting from scratch exposing something to POST data I'd most likely choose WebAPI nowadays.
Page_Load()
If the page has no content and its sole purpose is to receive these values, then Page_Load() would be the sensible place to capture the values and pass them along to wherever they need to go in the business logic.
I doub't Page_Load() fires because the page is not being physically opened
Sure it is, at least as far as the page itself is concerned. How the client requests the page and what the client does with the response from the page is immaterial in this regard. If the page is requested, it's "loaded" server-side and returned as the response.
I know it seems odd to leverage a .aspx page for this purpose, but that's the objective.
Very odd indeed. Though not uncommon. An ASHX handler may suit your needs more effectively, as might a WCF service endpoint. But without more information about what you're building and how it's going to be maintained, this is all hearsay.
I would like to know how the HTML source of ajax based sites can be read using HttpWebRequest / HttpWebResponse (That is reading the contents of a website at server side). The problem that I'm facing is that I'm unable to read parts of the webpage which uses Ajax or stuffs like UpdatePanel.
My application is in ASP.NET / C#, so can't think of using stuffs like Browser control or mshtml.dll since I would not be able to serve multiple requests.
Thanks in advance.
this is going to be difficult.
I know you said you don't want to use Browser control, but I'm going to say it anyway. You will most probably be better off using a Browser control. The reasons are as follow:
AJAX sites make multiple calls from the browser to the server to obtain the required view.
The multiple calls are being performed via JavaScript
The data returned from the server may be reformatted by JavaScript before being updated onto the view.
If you are going to do this using HttpWebXyz functions, you will have to do the following:
Make the relevant calls to get the initial page source.
Parse the page for JavaScript.
Evaluate/execute the JavaScript. This may include providing the relevant implementation for functions such as alert and making subsequent calls to the server.
Depending on the complexity of the AJAX site, you may want to reconsider using the browser control. Complex sites are easier process by the control. If the site is simple enough, you may survive parsing and executing the required JavaScript.
This example uses a deprecated class to parse JavaScript.
You may want to explore ICodeCompiler and its relevant classes for the new approach.
Good luck.
I have a ASP.NET MVC project and i want to use jQuery on them. as other mention I put the jQuery on head section and found that they will work in firebug.
Now I want to jQuery in my C# class. how i can use them in C# class. I want to run the code but it's never compile where I goes wrong.
public class Manager
{
public static void Test()
{
// i put here jQuery code but they never compiler i try many time.
}
}
what is the right way to use jQuery in C# class. like in javascript code is work if I write but in c# when I want to try something like ajax request.
$.ajax is work fine in javascript but when I want to run them in C# they not compile. What is the right way to send ajax request from c# class.
Please tell me the way I can use jQuery ajax function in c# class.
The main reason you can't is because jQuery is a JavaScript library, not a C# library so it just won't work. However, I'm not sure why you would want to do that.
Your C# code is running server-side, so what does an AJAX request even mean in that context? You are already running code on the server, there is no need to remotely contact the server over HTTP. Just run whatever C# code you need to get the data you want. Using AJAX would be kind of like trying to call yourself on the telephone to ask yourself something.
I think you have a fundamental misunderstanding of how the web works. Javascript (JQuery is just a Javascript library) runs in the browser. Your C# code runs on the server.
The browser makes a request for a page from the server, which is your C# code (well, your code + whatever the ASP.NET MVC framework does for you) which sends a page to the client. This page can include Javascript which the browser executes.
Your server side code doesn't run Javascript (unless you're using a Javascript based server like Node.JS instead of ASP.NET). It can output javascript for the browser to run, but it itself does not run it.
Now as far making a AJAX request from C#, if you're trying to call something on your own site #JohnFx is correct that it would be pointless because you can just call the code directly without making a request.
If you're trying to fetch data from an external site, you can make an HttpRequest from C# as shown here. There may be some wrapper code that makes it easier to work with, but I don't know any off the top of my head (it's not something that's too commonly done). You'll then need to figure out how to parse the response.
JQuery is a javascript library and therefore JQuery code cannot be put into your C# code. In order for your JQuery to run, you must output it with the rest of your html and it will run in the users browser. JQuery is javascript and cannot run on the server with your C# code.
If you'd like to make a web request to a web page from c#, similar to what happens when you do an Ajax request from jQuery, look at the HttpWebRequest and HttpWebResponse classes.
This link is also a good place to start to learn a bit more.
As the other answers point out, you simply can't execute jQuery code from c# (and you shoulnd't) since jQuery is not a technology that's meant to run inside of ASP.NET, on the server. But instead, jQuery is a library of Javascript code that makes it easier to write Javascript scripts, which execute inside the user's browser. Everything that you can do with jQuery, you can also do with pure Javascript since at the core jQuery is just a collection of javascript functions and objects.
well, the jquery is a rich javascript library and javascript is a client side language. It should be used inside the dom.
c# is server side language which is rendered on the server when the request came into server.
You need to undrstand the difference between client side and server side. here is an example article
I am trying to evaluate javascript from html page based on HTML (I mean some of the variables in javascript function are using html tag id )
I need to use it multiple threads (Not UI) so this means that i can not use WebBroser.
I can not use Eval.JScriptEvaluate because the javascript depends on the html
Is there is any way to achieve this with this limitations
Maybe there is web Service that revives html as string and lets execute some specific script.
You'll need to parse and execute the script - there's a number of projects out there to help with this see:
Are there any .NET CLR/DLR implementations of ECMAScript?
Interpreting JavaScript outside of the browser?
Can you give more detail in your question. e.g. Do you have control over this page that you are trying to run (in which case, maybe you could solve your multiple thread requirements with HTML5 webworkers) and why do you need multiple threads?
Here is one link you might find interesting which, with some work on your side would allow you to load web pages into Rhino (a server side, javascript interpreter) and execute the javascript.
Another link to check out is HTMLunit, which also allows you to simulate a browser that can execute javascript code from within Java.
This is different problem for me. I used ASP.NET2.0 with AJAX,C#.
Regularly client make request from HTML (client side) to any class(.cs) file(server side). And then make updation on that class files through Object or in database.
But I want to make request from Class file (server side) to HTML file (client side) based on that Object.
I m also used HashTable for containing Key and related to that Object.
If you are not able to understand then I try to Explain you my problem differently, I will give you one simple Example :I m calling any function from javascript to my class function, now I want call from class function to javascript function.
please explain me in brief,
Please Help me.
You can actually change the contents of an UpdatePanel during a partial page postback. So if the client clicks something on your ASPX page which alters your object on the server-side, you can update the controls on the ASPX page during that round trip.
If you need to simply update your view occasionally with no user interaction, then the ASP.NET AJAX Timer control will help.
There are three methods for interacting with server side code from the client side:
Update panels: This uses a partial postback where the page essentially does a full postback to the server and then ignores everything except whats in the update panel then sends the result back.
Page methods - This might not be quite what you are looking for since page methods require that the method be static on the page itself.
Web service methods - These are the same web services you may use from your aspx code behind but you will need a script manager on the page to use them from javascript.