2

From what I have understand, a CallBack to the backend code would not cause the page to reload(like a PostBack would). However, I don't know how to accomplish a CallBack from my client side JavaScript.

Or is there an alternative way to run a backen function only once?

1
  • Take a look at jQuery's ajax, post and get method calls. Commented Oct 27, 2011 at 9:14

3 Answers 3

2

http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

3 Comments

So basically an ajax call to page.aspx/functionName ?
yes - tell the called page (or action in mvc) what you want via the url (utilize the querystring)
e.g. mypage.aspx?action=delete&id=2
2

Have a look at this article by Dave Ward -

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

It takes you through the process of setting up a call from jQuery to c# step-by-step

Comments

1

When you create an HTML object such as HTML Button add runat="server" as attribute, ASP .NET will create a javascript function __doPostBack. You can use this function to call server object.

<html>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="lblCounter" runat="server" Text="Counter:"></asp:Label>
        <br />
        <br />
        <input id="btnAddCounter" type="button" value="Add Counter" runat="server" />
        <br />
        <br />
        <a onclick="__doPostBack('btnAddCounter','')" style="cursor: pointer;">Invoke Postback (it's just like push the Button)</a>
    </form>
</body>
</html>

the code behind:

Partial Class test_testpostback
    Inherits System.Web.UI.Page

    Shared counter As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblCounter.Text = "[This will display counter]"
    End Sub

    Protected Sub btnAddCounter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCounter.ServerClick
        counter = counter + 1
        lblCounter.Text = "Counter: " & counter
    End Sub

End Class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.