Hi,

I am new to ASP.Net. I would like to know if there is a way by which I can Call a Serverside script through a Javascript. Or else is there a way to Postback a page through a javascript function.

Thanks in advance

Thanks,
NaveenChander

Dani AI

Generated

A practical, robust pattern for ’s scenario (detecting when someone really leaves and clearing their “logged‑in” flag) is: keep a server‑side timeout/heartbeat as the ground truth, and use a best‑effort client notification when the page is hidden/unloaded. The old frameset/window.open trick mentioned by works once, but it’s brittle; the modern, supported tools are the Page Visibility API plus a best‑effort send on page hide. The browser unload/beforeunload handlers are not reliable across platforms (especially mobile), so they must not be the only cleanup mechanism. (developer.mozilla.org)

Best‑effort client code: on visibility change (or pagehide) invoke a small, quick POST to a lightweight logout endpoint. Prefer navigator.sendBeacon for the final signal, fall back to fetch with keepalive, then an image ping as last resort. Example (conceptual):

function notifyLogout(sid){
  const body = JSON.stringify({ sessionId: sid });
  if (navigator.sendBeacon) {
    navigator.sendBeacon('/session/logout', body);
  } else if (window.fetch) {
    fetch('/session/logout', { method: 'POST', body, keepalive: true, headers: {'Content-Type':'application/json'} });
  } else {
    new Image().src = '/session/logout.ashx?sid=' + encodeURIComponent(sid) + '&_=' + Date.now();
  }
}
document.addEventListener('visibilitychange', () => {
  if (document.visibilityState === 'hidden') notifyLogout(SID);
});

navigator.sendBeacon and fetch keepalive are specifically designed for these “send-on-exit” cases; fetch keepalive has a small payload limit and lets other HTTP methods when needed. Use visibilitychange/pagehide rather than relying only on unload/beforeunload. (developer.mozilla.org)

Server side (ASP.NET): expose a tiny, idempotent endpoint that updates the DB quickly and returns 200. Options: a Generic Handler (.ashx) or an AJAX WebMethod / page method (ScriptManager + [WebMethod]) — both are appropriate for a small logout ping; keep the handler simple so it responds fast. Also avoid synchronous XHR on unload — it’s deprecated and unreliable. (learn.microsoft.com)

Notes: always combine the client ping with a server heartbeat/timeout (e.g., periodic keepalive every few minutes). Treat the client ping as “best effort” and let the server clear stale sessions after the timeout window.

Recommended Answers

All 10 Replies

I used a query string from Javascript ,something like

Var windid = window.open("default.aspx?frameheight="+mapframeheight+"&framewidth="+mapframewidth+"");

Which sends these values to the server.

Hope it helps.

Hi Mr White Tiger, :-)

Thanks for your quick response.

Yes, this sends information to the server and not the server side script. Actually your code will try to load a new page from the URL specified but does not post back the calling aspx page or invoke an event in the Server side script. This is what I need.

Let me explain you the need (May be this will help you understand the Problem better),

Say you have an E-Mail service like Yahoo and hotmail. When a user logs into his account I will make an entry in the database saying that "User X has logged in will Blah blah ...Time... This can be done through the Page load event.

If another user tries to login to the same E-mail Account. I should be able to throw him out saying "Get out Mr.Y...

The problem that I am currently facing is that... I am not able to keep track of the first user, i.e., if he is still using the window or not. This cannot be done through the server side script because we do not have a page_UnLoad event in Aspx.

Work Around:

I can keep a Log out hyperlink where I can check if the user has logged out or not. But the problem is what if the user closes the window abruptly with out using the Log out Hyperlink (you see there are few stinking users).

Our JavaScript will help us in this issue. It has a unload system function which when declared in the Body tag will be triggered when the page Closes. (This is simply fast)

But I am not able to send the information from the JavaScript to the database to remove the user’s record from the database. SO I have to call a server side event (or a postback to the calling page) to update the database. AND this is where I need your help.

Hope it make sense.

I know this mail is toooo Long. Kindly bare ;-)

Thanks,
NaveenChander

Yo FISH(I dont know the name of the fish but I remember seeing something similar in National Aquarium in Washington DC) ;-)

Its good you said your purpose in detail,your first post lacked description.

I had to do something similar, a couple of months ago.But my luck I used a frameset.It was intended for a different purpose(The client wanted me to hide the URL) but it really helped for passing values from javascript to server side scripting.

I used something like this whenever I wanted to execute something on the server side from a javascript.
window.open(""default.aspx?f=1"" ,""mainframe"");

On the server side I checked for value of f and executed some database code.

Since the Javascript is client - side and VB.NET or C#.NET is Server side script, I dont think you can call server side codes from inside Javascript.

There is always a workaround.
Somebody here might help you on that.

Hi Mr. White Tiger,

The Name of the fish is Dory a Cartoon character in the movie Finding NEMO.

Thanks a lot for your Help Man. That was indeed helpful. If you dont mind, can you explain what did you do with the frameset? So that I will try to tweak up the Code.

Thanks,
NaveenChnader

Frameset

With frameset, you can display more than one webform in the same browser window. Each frame is independent of the others.

Disadvantages:

1.Its a mess when you try tuning your frameset for different screen resolution.
2.Each browser has some compactibility issue,but I can be solved easily
3.The web developer must keep track of more webforms.
4.It is difficult to print the entire page.

Advantages:

1.Its really easy to use.
2.You can invoke a server script from a Javascript.

For my application I had three frames.
1.Logo
2.Left frame
3.Right frame

The Logo webform is my secret hide out.I have some hidden server side codes.

The left frame had various buttons like,

  1. compose
  2. edit profile
  3. Signout
  4. bla
  5. bla bla

when the user clicks these buttons,for example edit profile
I'll executed the server script for calling the corresponding page (Editpage.aspx) on the right frame by something like this..

Dim strReDirectToAnotherFrame As String = "<script>window.open(""edit.aspx"" ,""rightframe"");</script>"
Response.Write(strReDirectToAnotherFrame)
POSSIBLE WORK AROUND FOR YOU IF YOU USE FRAMES

When the user closes the window,you can invoke a javascript function and send a query string to another frame,lets say Logo.aspx.

Javascript side on leftpage to send a javascript to server script.

//leftpage.aspx..This function must be invoked when the user close the window.

function clearflag()
{
window.open(""logo.aspx?flag=1"" ,""Logo"");
}

On the server script,

//logo.aspx

Dim flag as string 
flag = Request.QueryString("flag")
If (flag = 1) then
//Connect to the database and update the table

End If                 

Hope this Helps.

Hi Mr. White Tiger,

The Name of the fish is Dory a Cartoon character in the movie Finding NEMO.

Thanks a lot for your Help Man. That was indeed helpful. If you dont mind, can you explain what did you do with the frameset? So that I will try to tweak up the Code.

Thanks,
NaveenChnader

Yo Mr.Tiger,

Thanks a lot dude. That was indeed helpful. Frameset is very useful indeed.

Thanks again for all your patience and efforts.

Thanks,
NaveenChander

No Problem

Hi All,

Here is a nice article presenting parameterized web methods calling from clientside.

cheers

Hi,

I am new to ASP.Net. I would like to know if there is a way by which I can Call a Serverside script through a Javascript. Or else is there a way to Postback a page through a javascript function.

Thanks in advance

Thanks,
NaveenChander

use ClientScript.RegisterClientScriptBlock
to register ur scipt or u may do this as
use asp:button and on onclick event do ur coding then hide it by writing down this code on load page

btnname.style.add("display","none")

then do this by javascript
function calingbtnevent()
{
document.getElementByid(btnname).click(); //btname server button id
or 
u may do this as well
document.getElementByid('<%=btnname.ClientID%>').click(); 
}

use this function as ur requiremnt
Happy coding

i wrote one script function in source code in asp.net with c# coding use this pages,how do i can access the function in code behind side (Ex .. cs page)?

ex : page.registerstartupscript()

pls help me

by chandran

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.