Reputation: 3499
I have to fire a method in my asp.net page. Condition is that this method has to fire when page is loaded in client’s browser and client can see it. What I can do in server side and what I can do in client side ?
Upvotes: 3
Views: 2470
Reputation: 13672
You can use JQuery to determine if the page is loaded very easily client-side;
$(document).ready(function()
{
//page is fully loaded and ready, do stuff here
}
Server-Side: You can utilize a combination of a WebMethod, JSON, and Javascript (AJAX)
Concept Client-Side:
//////INLINE YOUR ASPX PAGE
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function ConsumeWebService()
{
SampleService.TehMethod();
}
$(document).ready(function()
{
//page is fully loaded and ready, do stuff here
}
</script>
//////////////////
Concept Server-Side:
<%@ WebService Language="C#" Class="SampleService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SampleService: System.Web.Services.WebService
{
[WebMethod]
public void TehMethod()
{
//do stuff server-side here
}
}
Here is a more in depth article on the process http://msdn.microsoft.com/en-us/library/bb515101.aspx
Upvotes: 2
Reputation: 49413
It sounds like the quickest and easiest way for you to achieve to automatically run server-side code is to wrap part of your page in an UpdatePanel and the set a Timer control to trigger the UpdatePanel to postback and run your code.
See this link for an example of using the Timer control to trigger an UpdatePanel
The above solution will load the page and then your client see the page appear to be updated dynamically. Now, if you really need your client to see what is going on then once the page loads, you will need to run code on the client that calls a webservice on the server that will run your code.
Upvotes: 4
Reputation: 853
You could also decorate the your method as a webmethod and call it via javascript when the page has finished loading.
Upvotes: 4
Reputation: 887365
The server doesn't know when the browser shows the page.
However, you can include an image pointing to a server-side script.
On the client, you can use the load
event.
Upvotes: 4