简体   繁体   English

在ASPX网页中从背后调用函数

[英]Calling a function from behind in an aspx webpage

I have this hidden button that is linked to a function I would like to trigger automatically on the web page once the session has been timed out: 会话超时后,我有一个隐藏的按钮链接到我想在网页上自动触发的功能:

Button code: 按钮代码:

<asp:Button runat="server" id ="btnHdn"  Style="display: none" OnClick="Button1_Click"/>

Timeout session trigger: 超时会话触发:

setTimeout(function ()
{
  document.getElementById("btnHdn").click;
}, timeout);

The code behind I would like to call (.cs): 我想调用的代码(.cs):

protected void Button1_Click(object sender, EventArgs e)
{
    RedirectToHomePage();
}

protected void RedirectToHomePage()
{
    try
    {
        string whichCountry = ConfigurationManager.AppSettings["country"].ToString();
        string isFromGMAL = ConfigurationManager.AppSettings["GMAL"].ToString();
        string urlStr = "LanguageSelection.aspx";
        if (whichCountry.ToLower() == "sg")
        {
            urlStr = "LandingPage.aspx";
        }
        else if (whichCountry.ToLower() == "nl")
        {
            urlStr = "LandingPagexxxx.aspx";
        }
    }
    catch (System.Exception ex)
    {
    }
}

Apparently, the code does not seem to be working. 显然,该代码似乎无法正常工作。 Once the session has reached zero, nothing happened and the timer will keep continue on counting. 一旦会话数达到零,则什么也没有发生,计时器将继续计数。

Would you guys please identify what is wrong with the code? 你们能否确定代码出了什么问题?

You just need to change little bit on JavaScript code cause btnHdn button ClientId is change on client side so use it like below and .click is a method so add brackets on it like .click() 您只需要在JavaScript代码上进行一些更改即可,因为btnHdn按钮ClientId在客户端已更改,因此请按以下方式使用, .click是一种方法,因此请在其上添加方括号,如.click()

setTimeout(function () {
   document.getElementById("<%=btnHdn.ClientID%>").click();
}, timeout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM