开发者

Javascript function using code behind called onPageLoad instead onClick

I h开发者_如何学Pythonave an HTML button that onClick should invoke two functions:

<input id="btnLogout" onclick="test();SaveAndNavigate('logoff.aspx');" type="button">

In the function test() I simply need to write/append in a .txt file (on the Server side) the username. The txt file is a log file and I want to keep track when the current user logs out.

The function test is:

function test()
{
<%
   LogUserSession("User: "+ System.Web.HttpContext.Current.ApplicationInstance.Session.SessionID.CurrentUser +" logged out on: " + System.DateTime.Now );
%> 
}

When I load the page with the button, the function is executed immediately on pageload instead of onClick event. Since what I need to achieve is already all implemented by the codeBehind function "LogUserSession", I would like to avoid having to develop jQuery Ajax web services or other solutions, if possible.

Otherwise how can I append text info to an existing .txt file from javascript? Thanks in advance.


Your LogUserSession() is a server side function thats getting executed when your page (containing the extracts you provided) is being generated - on the server side. It is not executing it as part of the onPageLoad event for the page (i.e. on the client side).

Basically what you want is to call the LogUserSession() function from logoff.aspx.

If your SaveAndNavigate() function works, then that should be all you need in your btnLogout. If you don't want to use AJAX to make the call, youll need to have a form that submits to logoff.aspx. Or even just a link to it, if it handles GET requests.


If i understand you correctly it is a server side function that you want to run. From the client but since the server parses the code before sending it to the browser it happens before the page reaches the browser. I think ajax is your best option.


The code between the <% %> is run on the server when the page is generated. This is actually happening before the browser event gets page.

I would reccomend you move the loggin of a logout into the "logoff.aspx" page. This way it is never forgotten.


One way of doing it is(not verified):

1) Have a page called Logout.aspx and in the page load event call/implement the functionality of LogUserSession method.

2) Add an img element on the page with width=0 and height=0

3) In the test javascript function assign the src attribute of the img to the Logout.aspx.

i.e:

<img id="logoutImg" width=0 height=0 style="display:none"/>
.
.
.
function test(){
 document.getElementById("logoutImg").src = ("Logout.aspx?_rnd" + new Date());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜