Saturday, March 24, 2012

AJAX Timer not Ticking in MasterPages

Hi,

I have a timer control of the new AJAX version released. When in a page that doesn't have any MasterPages the Timer works perfectly, however when you give a MasterPage to the Page the Timer never Ticks!!!... I put a ScriptManagerProxy in the aspx page and a ScriptManager in the MasterPage (followed exactly the example on this site), but still to no avail.

why is this ? is this a bug ? am I missing something else ? anyone encountered something the sort?

Thanks!

Can you post some code which doesn't work? What is a purpose of the ScriptManagerProxy? I wrote a simple example for Timer and master page and I didn't find any problems.

<%@. Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Untitled Page" %
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer ID="Timer1" runat="server" Interval="5000">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="Load_Some_Data" UpdateMode="Conditional">
<triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</triggers>
<contenttemplate>
<p>Refresh number: <asp:Literal ID="lit1" runat="server">0</asp:Literal></p>
</contenttemplate>
</asp:UpdatePanel>
</asp:Content>

public partial class Default2 : System.Web.UI.Page
{
protected void Load_Some_Data(object sender, EventArgs e)
{
lit1.Text = GetSomeNumber();
}
private string GetSomeNumber()
{
int i;
if (!int.TryParse(lit1.Text, out i))
return "ERROR";
else
return (++i).ToString();
}
}


Hello and thanks for the reply..

Try this: the Timer has to start Disabled and Enable it at runtime by a click event or so... also I'm seeing you don't have a Tick event!?


Here is a solution when Timer is by default disabled and during runtime enabled. I don't have tick event because updatepanel is refreshed every 5 seconds and on every refresh method Load_Some_Data is executed. What are you doing in your tick event and it can't be done this way?

<%@. Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer ID="Timer1" runat="server" Interval="5000" Enabled="false">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="Load_Some_Data" UpdateMode="Conditional">
<triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</triggers>
<contenttemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<p>Refresh number: <asp:Literal ID="lit1" runat="server">0</asp:Literal></p>
</contenttemplate>
</asp:UpdatePanel>

<asp:Button ID="Button1" runat="server" Text="Enable Timer" OnClick="Button1_Click" />
</asp:Content>

public partial class Default3 : System.Web.UI.Page
{
protected void Load_Some_Data(object sender, EventArgs e)
{
lit1.Text = GetSomeNumber();
TextBox1.Text = lit1.Text;
}
private string GetSomeNumber()
{
int i;
if (!int.TryParse(lit1.Text, out i))
return "ERROR";
else
return (++i).ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}
}


hey thanks bsebo for following my case...

currently I don't have my code in front have to get home. 3hrs more :\....

basically the situation is this: A ScriptManager in the MasterPage, ScriptManagerProxy in the Page. A disabled Timer on the Page and on Button click event is gets Enabled. Ok it gets "Enabled" BUT the Tick Event is never executed.. .while without a MasterPage it gets executed. I need to work with the Tick event as I'm doing some process on the Tick event and thats it! ... I will post the code when I get back home.

Thanks again!


<%

@.PageLanguage="C#"MasterPageFile="~/MasterPage.master"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2"Title="Untitled Page" %>

<

asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server"><asp:ScriptManagerid="ScriptManager1"runat="server"></asp:ScriptManager><asp:UpdatePanelid="UpdatePanel1"runat="server"><contenttemplate>

<

asp:Buttonid="Button1"onclick="Button1_Click"runat="server"Text="Start"></asp:Button><asp:Labelid="Label1"runat="server"Text="Label"></asp:Label>

</

contenttemplate><triggers>

<

asp:AsyncPostBackTriggerControlID="Timer1"EventName="Tick"></asp:AsyncPostBackTrigger>

</

triggers></asp:UpdatePanel><asp:Timerid="Timer1"runat="server"Enabled="False"Interval="5000"OnTick="Timer1_Tick"></asp:Timer>

</

asp:Content>

public

partialclassDefault2 : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

}

protectedvoid Button1_Click(object sender,EventArgs e)

{

Timer1.Enabled =

true;

Label1.Text =

"Timer started";

}

protectedvoid Timer1_Tick(object sender,EventArgs e)

{

Timer1.Enabled =

false;

Label1.Text =

"Timer stopped";

}

}

Hi, I'm back, managed to post the code... as you can see the Page has a MasterPage. ... while this peace of code works in a Page without a MasterPage...with a MasterPage it would start (or seem to be Enabled) but would never Tick. This is a bug for sure.... I'm testing right now with many possibilities but neither would work..

In fact something really weird and shows more that this is a BUG... is that when on debug the Timer is set to Enabled it will start Ticking but not when started from a Button.

bsebo you have to provide an example similar to what I said... thanks but yours is not what I want... bsebo try doing it with a Tick event ;)... your not using the Timer properly.

Thanks! martin


You right, it doesn't work. If you put the Button1 outside of the UpdatePanel1 then it can enable the Timer1. But, if you put it inside of the UpdatePanel1 then it can't. It seems like a bug because when you look at a:

<script type="text/javascript">
<!--
Sys.Application.add_init(function() {
$create(Sys.UI._Timer, {"enabled":false,"interval":5000}, null, null, $get('ctl00$ContentPlaceHolder1$Timer1'));
});
// -->
 enabled is always set to false.

</script>


It doesn't work yes... but are you sure because of that? I tried on a Page without a MasterPage and viewed the source while a Timer1 was enabled... it still showed that it was "enabled":false.

There's sure a bug... I hope some from the ASP team reads this!

I cannot quite get a grasp on what's happening.. why should a MasterPage conflict that much?

Thanks!


Hello so is there any work arounds?... or have to wait for the fix?

No comments:

Post a Comment