FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » General » HTML, Javascript, jQuery & AJAX » How to call server side function from javascript
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
How to call server side function from javascript [message #168493] Fri, 10 May 2013 08:52 Go to next message
Jack Hard is currently offline  Jack Hard
Messages: 7
Registered: December 2012
Karma: 0
Junior Member


i am looking to set sorting on ASP Repeater. see my repeater code:

<asp:Repeater runat="server" ID="RptClientDetails">
<HeaderTemplate>
<table id="example" class="dynamicTable table table-striped table-bordered table-primary">
<thead>
<tr>
<th>

<a href="#" onclick="ClientSort('ClientID')">Client ID</a>


</th>
<th>
<a href="#" onclick="ClientSort('Name')">Name</a>


</th>
<th>
<a href="#" onclick="ClientSort('TotalBalanceDue')">Total Balance Due</a>


</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>

here, i am calling javascript function. see my javascript function code:

function ClientSort(SortExpress) {

<%= Sorting(SortExpress) %>
}

from here i want to call my .net server side function.

public void Sorting(string SortExpression)
{
string s = SortExpression;

}

so, have you idea how can i call it? or directly from repeater i can call this server side function..Thanks
Re: How to call server side function from javascript [message #168606 is a reply to message #168493] Fri, 31 May 2013 12:04 Go to previous messageGo to next message
annaharris is currently offline  annaharris   India
Messages: 4
Registered: May 2013
Karma: 0
Junior Member
This might help! stackoverflow.com/questions/16475461/how-to-call-server-side-function-from- javascript
Re: How to call server side function from javascript [message #188107 is a reply to message #168606] Wed, 12 May 2021 11:24 Go to previous message
steveskok is currently offline  steveskok
Messages: 2
Registered: April 2021
Karma: 0
Junior Member
In order to call a Server Side function from JavaScript, it must be declared as static (C#) and Shared (VB.Net) and is decorated with WebMethod attribute.

Then the Server Side method can be easily called with the help of ASP.Net AJAX PageMethods and JavaScript without PostBack in ASP.Net.

Enabling PageMethods

The first thing you need to do is add a ASP.Net AJAX ScriptManager to the page and set its EnablePageMethods property to true.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>


HTML Markup
The following HTML Markup consists of an ASP.Net TextBox, an HTML Button and an ASP.Net AJAX ScriptManager.
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()"/>
</div>
</form>

Client Side Script
When user enter his name and Button is clicked the ShowCurrentTime JavaScript method is executed to get the Current Time.
The ShowCurrentTime method makes an AJAX call to the server using ASP.Net AJAX ScriptManager PageMethods and executes the GetCurrentTime method which accepts the username and returns a string value.
<script type="text/javascript">
function ShowCurrentTime() {
PageMethods.GetCurrentTime(document.getElementById("<%=txtUserName.ClientID% >").value, OnSuccess);
}
function OnSuccess(response, userContext, methodName) {
alert(response);
}
</script>


The WebMethod
The following WebMethod returns a greeting message to the user along with the current server time. An important thing to note is that the method is declared as static (C#) and Shared (VB.Net) and is decorated with WebMethod attribute, unless you do this you won’t be able to call the methods using ASP.Net AJAX ScriptManager PageMethods.
C#
[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}

VB.Net
<System.Web.Services.WebMethod()> _
Public Shared Function GetCurrentTime(ByVal name As String) As String
Return "Hello " & name & Environment.NewLine & "The Current Time is: " & _
DateTime.Now.ToString()
End Function
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Which code block is using bubble sort?
Next Topic: How many CSS can a website have?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Mar 29 14:36:27 GMT 2024

Total time taken to generate the page: 0.02839 seconds