0

Code is like given below, i am using Timer's event handler "ElapsedEventHandler" to call Response.AppendHeader or Response.Redirect methods, but i am getting "Object reference not set to an instance of an object" on the Response object. Anyone who can help on this ??

Want to set the refresh time dynamically then that can be done in ASP.NET by adding server side code

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    const double interval60Minutes = 60 * 60 * 1000; // milliseconds to one hour

    Timer checkForTime = new Timer(interval60Minutes);

    //checkForTime.
    checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
    checkForTime.Enabled = true;
  }
}

public void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
  if (timeIsReady(DateTime.Now.ToString("hh:mm tt").ToLower())) //12 Hour Date Format : 03:01 PM
  {
    System.Web.HttpContext.Current.Response.AppendHeader("Refresh", "2;Slider.aspx");

    //Or
    //System.Web.HttpContext.Current.Response.Redirect("Slider.aspx");
  }
}

private bool timeIsReady(string time)
{
  if (time.Contains("07:06 am"))
    return true;
  else if (time.Contains("08:06 am"))
    return true;
  else if (time.Contains("09:06 am"))
    return true;
  else if (time.Contains("10:06 am"))
    return true;
  else if (time.Contains("11:06 am"))
    return true;
  else if (time.Contains("00:06 pm"))
    return true;
  else if (time.Contains("01:06 pm"))
    return true;
  else if (time.Contains("02:06 pm"))
    return true;
  else if (time.Contains("03:06 pm"))
    return true;
  else if (time.Contains("04:06 pm"))
    return true;
  else if (time.Contains("05:06 pm"))
    return true;
  else if (time.Contains("06:06 pm"))
    return true;
  else if (time.Contains("07:06 pm"))
    return true;
  else if (time.Contains("08:06 pm"))
    return true;
  else if (time.Contains("09:06 pm"))
    return true;
  else
    return false;
}
samj28
  • 267
  • 2
  • 4
  • 10

1 Answers1

0

I haven't personally used the Timer control but in general feel that the Ajax Extensions and UpdatePanels are broken, outdated and should be avoided. This is an approach we took years ago. It defines a custom control that you need to add to your page. (You can't just instantiate it.)

You will need to modify the seconds value and register the control etc. I have no idea if this control will work within an UpdatePanel but I doubt it.

[ToolboxData("<{0}:PostBackTimer runat=\"server\" />"), DefaultProperty("Seconds"), DefaultEvent("Timeout")]
public class PostBackTimer : Control, IPostBackEventHandler
{
    public PostBackTimer() { }

    [Description("PostBackTimer_OnTimeout")]
    public event EventHandler Timeout = delegate { };

    public void RaisePostBackEvent(string eventArgument)
    {
        Timeout(this, EventArgs.Empty);
    }

    protected override void OnPreRender(EventArgs e)
    {
        var seconds = 45;

        var postback = Page.ClientScript.GetPostBackEventReference(this, null);
        var script = string.Format("setTimeout(\"{0}\",{1});", postback, seconds * 1000);
        Page.ClientScript.RegisterStartupScript(GetType(), "PostBackTimer_" + UniqueID, script, true);

        base.OnPreRender(e);
    }
}
andleer
  • 22,388
  • 8
  • 62
  • 82