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

时间:2018-12-18 04:24:11

标签: c# .net

我有一个隐藏的按钮,该按钮链接到我希望在会话超时后自动在网页上触发的功能:

按钮代码:

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

超时会话触发器:

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

我想调用(.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)
    {
    }
}

显然,该代码似乎无法正常工作。一旦会话数达到零,则什么也没有发生,计时器将继续计数。

请您确定代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

您只需要对JavaScript代码进行一些更改,因为btnHdn按钮ClientId在客户端已更改,因此请按以下方式使用,而.click是一种方法,因此请在其上加上括号,如{{1} }

.click()