简体   繁体   中英

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):

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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