Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a tricky problem. I am in a situation where I need to use a method defined in a .cs file from a javascript function. The problem is we are using .NET 1.1 and AJAX cannot be used for the application.

Also, I will need to pass a string from the javascript to the server side method. The page where I am implementing the javascript is a .as Any ideas?

I have tried doing a post back and creating a RaisePostBack event handler method (both in the .aspx page and the .ascx user control) but no luck.

It will be greatly appreciated if someone could help me out with the problem.

To be more precise. I have a class Attachment.cs which has a method Delete().

The javascript is called from a span's onclick event. The javascript function's input parameter would be a string which I will need to use to instantiate an Attachment.

I created a method which instantiates an Attachment using a string and calls the corresponding Delete() method for the Attachment object.

Now, I will need to pass the string from javascript function to the method I have created. I cannot use PageMethods.

The Javascript function is like: // File ID is a string

function DeleteAtt(fileID) 
{
     //I tried PageMethods here, tried posting the page and getting the value through
     // a RaisePostBack event etc. No luck.
}

The cs method I created is like:

public void DeleteAttachment(string ID)
{
   Attachment obj = new Attachment(ID);
   obj.Delete();
}
share|improve this question
1  
Ajax is a catch all term for "Making an HTTP request from JS without leaving the page". Unless leaving the page is an option, it sounds like you are asking how to do Ajax without doing Ajax. – Quentin Mar 9 '11 at 16:18
@David. Exactly. An alternative for PageMethods to be more precise. – Aswin Ramakrishnan Mar 9 '11 at 16:33
add comment (requires an account with 50 reputation)

3 Answers

You can inject an invisible Iframe into the page and the SRC of the iframe can be used to pass data back to the server.

This is a very old technique back before jQuery and Prototype were invented.

See: http://www.ashleyit.com/rs/ for other alternatives.

share|improve this answer
add comment (requires an account with 50 reputation)

Do you mean that Microsoft's "ASP AJAX" is not an option, or do you mean that any jquery/any other library, or your own hand written javascript ajax won't work? ASP AJAX may not be supported by you version of .net, but surely simple javascript will still work, as you want to access the page from javascript.

If this is the case, something as simple as this, using jquery, would work:

function submit() {
    $.post(
        "pagename.aspx?string=" + variable,
        function (data) {
            alert("Response: " + data); 
    });
}
share|improve this answer
@Michael. AJAX is not an option. There were some issues while implementing with AJAX and a higher .NET version before it seems. – Aswin Ramakrishnan Mar 9 '11 at 16:29
Could you explain this? Ajax isn't a different language - ajax is just a way of implementing pure javascript. In what type of situation could you be using javascript to make the request, but not be able to use javascript to create an asynchronous call? – Michael Jasper Mar 9 '11 at 16:34
@Michael. I understand very well that AJAX isn't a different language and I have used it before. The application I am working on is in production from 2005 (long long ago..). What I am doing now is like a support kinda thing. The team that worked on the project tried implementing some AJAX on the application and faced some issues where some other functionalities didn't work very well. So this what I was told: "NO AJAX.." – Aswin Ramakrishnan Mar 9 '11 at 16:44
Ah... Sounds like a management may have a misunderstanding of what ajax is, as pagemethods, as well as other .net functionality ARE ajax - just microsofts branded flavor. If they are using pagemethods/etc.. they are using ajax without even knowing it. – Michael Jasper Mar 9 '11 at 16:49
@Michael.. Well I guess you got me wrong. PageMethods cannot be used. I and the management very well know PageMethods are ASP AJAX methods :) What I am looking for is an alternative for PageMethods without actually using any of its libraries. – Aswin Ramakrishnan Mar 9 '11 at 16:53
show 3 more commentsadd comment (requires an account with 50 reputation)

How about adding a callback-url as a query string to the url that you need to submit data to? Example:

Initial Page:(javascript)

function send(){
    window.location = "submiturl.aspx?str=var&
                       callbackurl=http://www.myurl.com/page.aspx";
}

submiturl.aspx then catches both query strings [str] and [callbackurl], performs the action/function and then sends a response back to the [callbackurl]

response.redirect(callbackurl + "?response=" + data);

Initial Page now uses response.querystring to determine if it was succesful/whatever else you want and continues its business.

This definitely does not use ajax, and would be, by no means, asynchronous, but is based on a pretty lengthy history of API/callback & response design.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.