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?

Naning
  • 734
  • 2
  • 8
  • 18

3 Answers3

2

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

jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
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

ipr101
  • 24,096
  • 8
  • 59
  • 61
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
Ahmad Firdaus
  • 61
  • 1
  • 4