make jquery ajax calls to asp.net page - c#

How do I go about making true ajax requests to an asp.net page? (Not update panels). I read this tutorial but couldn't get it working. Is there a better approach? Or should this work?
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

The standard today is using JSON to get the job done. That means you'll be using JavaScript, and when you're using JavaScript jQuery is your friend.
Here's some example code that uses AJAX, jQuery, and a .NET Web Service. Learn it, love it, live it.

Try PageMethods. It will be easier. You're living in a .NET world, use the .NET tools.
Here's a page to get you started.
http://aspalliance.com/1922

Related

Creating a Chatbox with AJAX, HTML and C#?

I am using the Nancy Web Framework in my C# Console Application to basically create a Web Administration panel for my software. I have opted to use the Spark View Engine, as it is basically just HTML. I basically want to create a chatbox, except pull the data written to my application's console every X seconds and display it in a box instead.
I have very little experience with JQuery and AJAX, but they aren't overly complicated from the examples I have seen. The issue I am running into is that ALL of the chatbox and shoutbox examples use PHP.
I basically just need something like this...
The only difference is I need to pull the information from my application instead. I can use basic C# methods inside of the HTML (and probably inside of javascript but I haven't tried this). What would be the best way to do this, and are there any examples floating around that don't use PHP?
This was completed using AJAX and JSON.
Well, to use HTML for styling inside some PC program is just not wise. It has much better UI engines, though. But for your information here is nice jQuery shoutbox tutorial, but well, you only need to handle data input and output with C#, so actually I see no problems. The engine which you are using should have some kind of data stream, or requests handler (bla://program/???)

How to run jQuery in my C# class?

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

jquery and ASP.Net AJAX framework

I am developing using VSTS 2008 + C# + .Net 3.5 + ASP.Net. I am new to jQuery and ASP.Net AJAX framework. Suppose I need to use jQuery function (e.g. from jquery-1.3.2.js). I want to confirm that jQuery and ASP.Net AJAX framework are totally two different things, no dependencies?
So, no need to install and configure ASP.Net AJAX framework in order to use jQuery function?
thanks in advance,
George
Yes you are correct. You don't need both to function. You can find information on the AJAX toolkit here. You might also find JqueryUI quite useful. I'm quite new to Jquery as well and found JqueryUI to be quite a time saver. If you want, you can also let Google host Jquery and/or JqueryUI for you, so your bandwidth doesn't get taken up (Info here). Just to add, if you want intellisense with your Jquery code, this blog shows you how to setup VS2008 to do so.

Implementing UpdatePanel manually

I was reading an article that shows how bad CodePlex uses UpdatePanels and how nice is StackOverflow on this matter when, for example, a user upvotes an answer/question.
I wonder if someone can point a tutorial on how to do such action.
I know some points:
Create a Web Service that gets the action value and ouputs a JSON string
Build the javascript inside <ajax:ScripManager> control to replace the correct value on the page with the new value
But, even in the first I have difficulties, I can send a JSON string, but it will always be surrounded with XML information!
Can anyone (or maybe Jeff) point to a nice "how-to" since scratch? Thank you.
Well, I doubt StackOverflow uses UpdatePanel - more likely it uses jQuery / load to simply update a div, using ASP.NET MVC as the source (rather than ASP.NET vanilla, which has a more complex page cycle).
With this approach, it is trivial... the jQuery examples tab largely says it all.
Re returning the Json - that is simply return Json(obj); from the controller in ASP.NET MVC - but personally I'd return the html (simpler).
Before you dismiss the UpdatePanel I suggest you have a read of this post I did - http://www.aaron-powell.com/blog/august-2008/optimising-updatepanels.aspx. It looks at how to optimise UpdatePanels and it can lead to some performance increases if done well.
I also did a post - http://www.aaron-powell.com/blog/august-2008/paging-data-client-side.aspx which looks at doing client-side templating with jQuery and MS AJAX. I look at how to read a web service with JavaScript and if you download the sample you'll see how to send data client-side to a web service.
But this video cast on the ASP.NET website may also be of use - http://www.asp.net/learn/ajax-videos/video-82.aspx. It's on how to extend web services for script service capabilities.

textbox autocomplete

how do i make an autocomplete textbox in asp?
but i need to get the autocomplete data by querying the database.
I dont really know how to explain this, sory if theres not enough detail.
i cant use ajax, because i think i will have compability issues with my old app.
so im thinking of doing this using java script. or is there a way to do this by using .net?
im using C# for codebehind. thanks
It's going to be a lot of effort without using some third party autocomplete I think - not sure what you mean by 'I can't use ajax', but how about using the ASP.NET AJAX autocomplete control, setting the ServiceMethod property to a static Page Method in your code behind? That keeps it contained within your page at least.
The Page Method can go off to your database, and return a String[] of results.
If you decide to use it, you'll need to set the EnablePageMethods property to true in the <asp:ScriptManager> control.
AJAX is JavaScript. It's JavaScript using the XMLHttpRequest object to make the asynchronous request. Here's an article about it and ASP.NET.
If you want to know more about AJAX, (Asynchronous JavaScript and XML), I'd check out Wikipedia first. If you want books on it, there are a ton. I recommend Programming ASP.NET AJAX by Christian Wenz (O'Reilly And Associates).
if you dont want to uses ajax library, try jquery
there are many plugin autocomplete or suggest textbox for jquery
try this one
http://www.vulgarisoip.com/2007/08/06/jquerysuggest-11/
The ASP.NET AJAX framework works for ASP.NET 2.0 & above. As such it will not work in Visual Studio 2003 environment.
Anthem.NET is a free, cross-browser AJAX toolkit/framework for the ASP.NET development environment that works with both ASP.NET 1.1 and 2.0 -
http://sourceforge.net/projects/anthem-dot-net
For your autocomplete requirement, you can consider using the jQuery Autocomplete Plugin
It requires very less programming. Check the demo & code sample here - http://docs.jquery.com/Plugins/Autocomplete
It's autocomplete() method takes a URL or array to populate your autocompletion list. You can pass the URL of the page that fetches the results from the database directly.

Categories

Resources