How to implement A simple AJAX call using asp.net page? - asp.net

I'm trying to implement this specific example using asp.net page instead of asp page.
If you look at the example you can see that there are 2 parts for the page:
Mail asp page. This page have JS code that calls other asp file for AJAX use.
the other asp page which holds the JS code.
The responseText of the call is the client side code, so, when I write something like this:
<html><head><title>test</title><script language="javascript" runat="server"
type="text/javascript">function test(){Response.Write("This is a Test!");
</script><body onload="test()"></body></html>
the page ignores my server side code and returns this:
<html><head><title>test</title><body onload="test()"></body></html>
what should I need to do to make him process my JS code and return its output?
Thanks in advance,
Oz Radiano.

asp.net does not process javascript server side, so setting the script tag runat=server with language="javascript" will be mostly ignored.
I think if you change it to "JScript" it will work, however, this has nothing to do with ajax.
"runat = server" says, preprocess this on the server and don't send it to the client.
If the language is a processable one it will be evaluated as well.

Try implementing the example after watching some videos from http://www.asp.net/learn/ajax-videos/ and http://www.asp.net/learn/ajax/
Its very easy to implement AJAX in asp.net then ASP. I can clearly give you the correct source code. :) But you seem to try out new things. Let us know how it goes!

Thanks for your responses, they made me understand that I'm not sure what my problem is.
After failing in implementing this exact example, I've googleed "how to run asp code using ajax"
this result returned and made it very clear.
Thanks again.

Related

Checkboxes in ASP.NET

I am trying to use a checkbox that is dynamically declared in an .vb file that I am trying to write into my .aspx page. I am able to write a normal checkbox of <input type='checkbox /> from the .vb Class using Response.write, but it comes up blank when using <asp:Checkbox runat='server' />I need to pass whether or not the box is checked back to the server, because I am having to either approve something if one is checked, reject something if the other is checked or do nothing if neither are checked. I have figured out how to make them mutually exclusive either way so that is not the problem. Does anyone have any recommendations?
Your problem lies in the order that the pages are compiled in: When you place an asp control like the asp:checkbox, it is compiled into a regular checkbox with some javascript attached when it is sent over to the client.
When you write the string "<input type='checkbox />" to the page from the code-behind it is writing that string directly to the page, after the aspx page has compiled its controls, but since that is valid html the browser renders the control. When you write the asp:checkbox, the browser doesn't know what to do with it, because it is not valid html. In addition, the page has already been compiled, so there is no chance of .net creating the correct control for you.
You need to programmatic add the control to the webpage by creating a new control through the code behind
This site does a great job explaining it
And #toodles seems spot on. Writing static html and asp.net are two totally different ball games. I would spend a bit of time (like hours/days) reading/watching learning material to help you get on your feet.
The technical answers you are getting are all good. However, your question indicates that you really need to start by learning how asp.net server controls work. I suggest spending a couple hours watching the videos at http://asp.net and particularly http://www.asp.net/general/videos/intro-to-aspnet-controls
Then focus on understanding the page lifecycle and you'll have enough of the basics to be much more effective at asp.net. Have fun!
You can't use response.write to create server controls.
See this site for an example of the right way to do it:
http://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

Easiest way to simply display confirmation that a webservice worked?

I'm calling an asp.net webservice from an ASP clasic page basically just with:
<a href='http://domain/webservice.asmx/command'>Command</a>
and when users hit that button it works but they're just shown an xml page. The function will either work or not so I was wondering if it'd be possible to just have a pop up box appear to tell them if it worked or not after they clicked it rather than redirecting them to an xml page.
I'd prefer to not have to use jQuery or another javascript library.
If that's not possible, is there any way to dress up the XML page? Currently it says 'This XML file does not appear to have any style information associated with it. The document tree is shown below.' at the top.
Also, the domain that the webservice is on is different to the domain that the website that's call the webservice is on. Not sure if that matters.
Thanks
Check out this MSDN Link on Calling A WebService From Javascript Using AJAX. No JQuery is required and it boils down to having to use the ScriptService attribute on your WebService method and adding a ServiceReference in a ScriptManager control. You can then easily call your WebService from Javascript and it will call another Javascript function when it finishes. It is in that response function where you can add your confirmation display.
be aware that this is a bad idea to let the user handle directly - web services are almost always called by your code rather than a client browser session. One reason is that raw error information would be hown to the client if there were a problem.
If you really want to do this, you can either:
Use AJAX (No framework required - just JS) or
You can make the webservice non-standard so it returns user-friendly content - perhaps by wrapping it in a website which calls the API behind the scenes and formats the response in a meaningful fashion.

How to force a postback with asp.net and C#

I have a demo scheduled with a client and i need a quick and dirty fix for now. I will find a more appropriate work around tomorrow but for the time being i need a way to force a post back, or refresh the page.
i tried:
Response.Redirect("");
but it brings me to a page that says "Object moved to here". 'here' is a hyperlink that brings me to the page with desired results but i wish to bypass this message.
Any ideas.
Response.Redirect("default.aspx");
(or whatever the name of the current page is)
Response.Redirect(Request.RawURL);
This also works and you won't need to worry about putting in the path.
Response.Redirect() is not the greatest because there is not state. It's a new request. if you want to keep state of all your controls then use the __doPostBack method which is added automatically by ASP when the page is rendered so it's accessible from client side:
you can do this:
or just call it from javascript:
__doPostBack('myElementId','');
Alternatively you can just use javascript code:
document.forms[0].submit();
Couldn't you just add a javascript block with window.reload() in it?
Here is some useful info on how to do this correctly in Web Forms.
The server can not tell the client to reload.
You can use the html meta refresh:
<meta http-equiv="refresh" content="2;url=http://the.new.url">
but that will not do a proper post back i think.
Content is how many seconds the client waits to do the refresh.
Take a look at ASP.NET AJAX Timer Control!
http://www.asp.net/ajax/documentation/live/tutorials/IntroToTimerControl.aspx
Do you need a post back to populate a list? Did you look into if solving it with Ajax could help??
Or if you just need a quick and dirty thing, just fake it and fix it later.

ASP.NET jQuery Ajax posting forms

I can't seem to get the jQuery.ajax() function posting back any of my asp.net generated form controls.
I've put a break point on the server side and there aren't any values.
Is there a way around this or do I have to build up a list of what I want sent back?
Another question slightly off topic, but it seems that although jQuery is a great JS library, it doesn't seem to integrate too well with .net.
Has anyone given up with jQuery to perform server side interaction and just gone with ms ajax implementation?
The reason for this is because asp.net webforms doesn't use a normal post (ie. with an input/submit button). if you take a look at how those are posted, there is some javascript handler that ends up calling a built-in function that asp.net writes out to the page called __doPostBack.
Check out this other stackoverflow answer that might give you additional clues:
Jquery asp.net Button Click Event via ajax
To your second question, once you work out a few of the kinks, jQuery is a fantastic lib that has a ton of support and reference material both on the web and in books. Keep at it and you won't regret it :-)

AJAX call to asp from bookmarklet

I'm trying to create a bookmarklet that will start off an AJAX call to an aspx page I've written.
The code tests out perfectly when I place the javascript in a static html page, but when I try and call it off from a bookmarklet, the code will just hang at the xmlHttp.open("GET", url, true) part.
The code of the bookmarklet is basically this (found on several examples on the web):
javascript:(function(){
var s,
d=document,
a=function(o){ d.body.appendChild(o) };
s=d.createElement('script');
s.type='text/javascript';
s.src='http://localhost/squirt/sq.js';
a(s)
})();
This adds the contents of sq.js (the ajax call + some other processing) to whatever page the browser is currently at, and then calls off the ajax to my aspx page.
I'm using ASP 2.0 (with VS2008) and IIS 7. So far I've just been testing it on my home network.
I assume there must be some sort of permissions issue with the ajax call from an outside page, since, like I said, everything works fine from a static page. Is this an IIS setting I need to change to allow the call, or am I doing something completely wrong?
The XMLHttpRequest object is subject to a Same Origin Policy.
This is why the script your bookmarklet is loading can't use an XHR to get data from your server unless it's embedded in a page from your server.
Script added by dynamically adding a script tag will work though, as you can tell - your bookmarklet can load script from a different origin.
So there's your answer. Don't use an XMLHttpRequest object: dynamically load your script in the same way the bookmarklet does.
This is how JSONP works (actually there's a bit more to JSONP but that's how it gets around the SOP)
Actually, why not just use JSONP
Injecting JavaScript code on the page still has the same permission issues as code that is there normally. You can not make an Ajax call to a different domain. So if you are calling localhost from example.com, it is not going to work.
You might want to look at returning JSON from your service and make JSON calls with a script tag.
Eric
The code you're using there is rather ugly, I would suggest using something like this that I built: http://sktrdie.org/getScript.js
It works like this:
getScript("http://anotherdomain.com/something", function(data) {
alert(data); // the request is complete
});
On the http://anotherdomain.com/something it would have to return something like this, given you're using PHP:
echo $_GET["jsonp"]."('Testing data, you can put anything in here');";
Be sure to read about JSONP.

Resources