ASP Checkbox being checked after refresh

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?

Answers


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


Need Your Help

modify wix-made bundle binary

c# wix codedom

Here's my situation. I have a WiX bundle package with a custom installer UI. It has one MSI packaged into it (that has no UI). You can download that bundle from my server. I want to modify that bun...

WPF ScrollViewer show border when ScrollBar is Visible

wpf wpf-controls binding scrollviewer

i want to add a border to my scrollviewer. This order should only be shown when the ScrollBar of the ScrollViewer is Visible (VerticalScrollBarVisibility set to "Auto")