0

I want a detailed understanding of how these calls work. I know we can declare a service class in Web Forms which derives from System.Web.Services.WebService and functions inside this service class can be called from JavaScript after declaring function with this [WebMethod(EnableSession = true)].

I have aspx pages with their back end aspx.cs files which contains public classes only. Is there any way that I can call functions inside these aspx.cs files and inside those public classes. Static and non static functions both. If not what is the reason behind it and if yes, then how it works.

This post is to get a deeper understanding of structure and working of web forms.

2 Answers 2

1

The methods must be static. A static method is exactly the same for every page. Because it is a web page, multiple users can access it. If 4 users on your site have the page open and a method says 'Do x', it has to be able to do the exact same thing for everyone.

For example, a common misunderstanding with page methods: you cannot access controls on the page from a static WebMethod/Page Method. If the method says 'update this control', which control, on which page should be updated? User 1, 2, 3 or 4? It is impossible to know.

You must return the data/info from code-behind methods to the javascript method that called the method, then update your page's control(s) from the javascript method.

Hth.

More

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

4 Comments

I understood this part. However, we are able to access session at code behind when we called, if it's unable to update control not knowing whether it is user 1,2,3 or 4, then how it knows which session is this ? If it know session then why not update control. ?
Session is not part of the page, it's part of the application. See Global.asax. You can access if from a page but it's not the page. I think I know what you're saying but I think it has to do with being able to access the code behind from js that changes things.
If you post back a page, the server has everything to work with; if you access code behind without posting back the page, the server cannot assume anything about the page, it doesn't have enough info.
I was looking for a link that might help but the website is defunct. Not sure I can fully, properly explain it afterall. I get your question though.
0

It is not possible to call non static method from client side using javascript / jQuery. To call the non static methos using javascript you jave some different ways like using the web service, WCF service and etc.

I am going to explaing how to call non static method with web service using javascript:

First create a new web application in my case the application name is "WebApplicationDemo".

I have added webservice in this project named as "WebService1.asmx".

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplicationDemo
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld(string strName)
{
return "Hello " + strName + ", The current time is = " + DateTime.Now + "";
}
}
}

Build you project and then added a new webform named as "WebForm1.aspx".

code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplicationDemo.WebForm1" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/javascript">
function getTime() {
var name = document.getElementById("txtName").value;
WebApplicationDemo.WebService1.HelloWorld(name, getTimeSuccess, getTimeError);
}
function getTimeSuccess(result) {
alert(result);
}
function getTimeError(error) {
alert(error);
}

</script>

</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<div style="font-family: Arial">
<asp:Label runat="server" Text="Enter Your Name"></asp:Label>
<asp:TextBox runat="server" ID="txtName" Height="20" Width="150"></asp:TextBox>
<input type="button" id="btnGetTime" value="Submit" onclick="getTime()" />
</div>
</form>
</body>
</html>

Now run the project in web browser and you will get the textbox and button there in my case i have types "Purvesh" inside the textbox and press submit. It does invoke the service which contains my non static methos and get the result back.

OutPut

Hello Purvesh, The current time is = 18-Sep-15 4:04:10 PM

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.