So basically i have a ASP checkbox that handles auto refresh. If it is checked then the page will refresh after every 5 minutes, when its unchecked it will not refresh

I am getting issue with when I uncheck the checkbox the page still refreshes after 5 minutes even tho it shouldn't.

This is the ASP for the checkbox

    <asp:CheckBox ID="chkAutoRefresh" runat="server" AutoPostBack="True" style="position:absolute; top: 48px; left: 908px; height: 6px; font-family: Arial, Helvetica, sans-serif; font-size: 11px; right: 219px;"
                    Checked="true" Text="Auto refresh" />

This is the call

    <meta http-equiv="refresh" content="<%= GetRefreshTime() %>"/>

This is the code behind

    protected string GetRefreshTime()
    {
        if (chkAutoRefresh.Checked)
        {
            return ConfigurationSettings.AppSettings["AutoRefreshTime"]; //refresh for every 5 min
        }
        else
        {
            return string.Empty; //if not checked do not refresh
        }
    }

where am i going wrong?

有帮助吗?

解决方案

What you are missing here is that with using meta refresh your whole page is being reloaded. The data about checkbox, if it is checked or not, is lost (ViewState is emptied).

What you should do is save the checkbox checked data in some more persistent state, which is kept through reloads, Session object for example.

And then always use the value from the Session instead (of course if the user changes the checkbox value, change the value in Session also).

Example

HTML:

<asp:CheckBox OnCheckedChanged="chkAutoRefresh_CheckedChanged" ID="chkAutoRefresh" runat="server" AutoPostBack="True" Text="Auto refresh" />

CODE BEHIND:

protected void Page_Load(object sender, EventArgs e)
    {
        // on every "first" page load which is also every refresh through meta refresh tag
        // this one will not be executed when we click checkbox which is a true postback
        if (!IsPostBack)
        {
            Response.Write("Refreshed! " + DateTime.Now);
            AutoRefreshSite();
        }
    }

    protected void chkAutoRefresh_CheckedChanged(object sender, EventArgs e)
    {
        // store value into session
        Session["autorefresh"] = chkAutoRefresh.Checked;
        // call method where you enable/disable auto refresh
        AutoRefreshSite();
    }

    protected void AutoRefreshSite()
    {
        if (Session["autorefresh"] != null)
        {
            // append meta refresh tag
            if (bool.Parse(Session["autorefresh"].ToString()))
            {
                HtmlMeta tag = new HtmlMeta();
                tag.HttpEquiv = "refresh";
                tag.Content = "5";
                Header.Controls.Add(tag);
                chkAutoRefresh.Checked = true;
            }
        }
    }

其他提示

You cannot use <%= GetRefreshTime() %> to set content for <meta> tag.

So in this case you can use HtmlMeta object to write tag from Code-Bihide.

protected void chkAutoRefresh_CheckedChanged(object sender, EventArgs e)
{
        HtmlMeta tag = new HtmlMeta();
        tag.HttpEquiv = "refresh";

        if (chkAutoRefresh.Checked)
        {
            tag.Content = "your content";
        }
        else
        {
            tag.Content = "";
        }
        Header.Controls.Add(tag);
}

Hope it work fine. :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top