Mert
Mert

Reputation: 6572

calling c# method at ajax success

MessageBox is another ascx, I am trying to trigger Message method after ajax success but It triggers at page load.

MessageBox.ascx's page load is blank. and It has some reusable javascript/c# messagebox stuff.

    $.ajax({
        url: "/_Handler/DBFileUpload.ashx?ItemId=<%=ItemId%>",
        data: formData,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function (data) {
            <% MessageBox.Message(MessageBox.MessageType.Success, "Congratz!", "File uploaded.")%>
        },
        error: function (errorData) {

        }
    });

SOLUTION

as Igor pointed out, I return the javascript function so I can call directly at ajax success.

$.ajax({
    url: "/_Handler/DBFileUpload.ashx?ItemId=<%=ItemId%>",
    data: formData,
    processData: false,
    contentType: false,
    type: 'POST',
    success: function (data) {
        window[<%= MessageBox.MessageJS(MessageBox.MessageType.Success, "Congratz!", "File uploaded.")%>](arguments);
    },
    error: function (errorData) {

    }
   });

Upvotes: 0

Views: 778

Answers (3)

Igor
Igor

Reputation: 15893

You can output (when rendering the page server-side) javascript/html required to show a message box on the client, and trigger that javascript inside ajax.success.

Upvotes: 1

Nauman Ahmad
Nauman Ahmad

Reputation: 320

You can't. Javascript runs client side, C# runs server side.

In fact, your server will run all the C# code, generating Javascript. The Javascript then, is run in the browser. As said in the comments, the compiler doesn't know Javascript.

To call the functionality on your server, you'll have to use techniques such as AJAX..

 $.ajax({
       url: "/_Handler/DBFileUpload.ashx?ItemId=<%=ItemId%>",
      data: formData,
       processData: false,
       contentType: false,
        type: 'POST',
        success: function (data) {
              //if you have any data return from ajax call it's in data object

                 $("div").html("success");
        },
        error: function (errorData) {

      }
  });

Upvotes: 2

QBM5
QBM5

Reputation: 2788

I would first recommend you taking some online courses on client side development, because what you are trying would never work.
Secondly what you are trying to do is very easy using javascript alert;

http://www.w3schools.com/jsref/met_win_alert.asp

 $.ajax({
        url: "/_Handler/DBFileUpload.ashx?ItemId=<%=ItemId%>",
        data: formData,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function (data) {
           alert("File uploaded.")
        },
        error: function (errorData) {

        }
    });

Upvotes: 3

Related Questions