Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts

Monday, March 26, 2012

AJAX timer wont trigger UpdatePanel in Window Mobile 5 device

My webform WORKS FINE when published to its localhost site and I bring it up in IE7.

I have a GridView and a textbox inside an updatepanel and an AJAX timer to trigger the update.

When the AJAX timer ticks, the textbox receives the current time and the Gridview updates from its SqlDataSource.

BUT ... when I try to execute it on 1) the Pocket PC Windows Mobile 5 Emulator OR 2) on a real PPC with WM5, it just sits there static after the initial load. Manual refreshes work but the timer appears to do nothing.

I would appreciate any ideas, especially any methods to help debug the client behavior to see if that timer is indeed doing anything.

<asp:ScriptManagerID="ScrMgr1"runat="server"EnablePartialRendering="true"/><asp:TimerID="Timer1"runat="server"OnTick="Timer1_Tick1"Interval="5000"></asp:Timer><asp:UpdatePanelID="UpdatePanel1"runat="server"UpdateMode="Conditional">
<Triggers> <asp:AsyncPostBackTriggerControlID="Timer1"EventName="Tick"/></Triggers><ContentTemplate> <asp:GridViewID="GridView1"runat="server"DataSourceID="SqlDataSource1"></asp:GridView> <asp:LabelID="Label1"runat="server"Height="82px"Text="Label"Width="306px"></asp:Label> <asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NautilusDBConnectionString %>"
SelectCommand="SELECT [Source], [Loads] FROM [Loads]" ProviderName="<%$ ConnectionStrings:NautilusDBConnectionString.ProviderName %>"></asp:SqlDataSource></ContentTemplate></asp:UpdatePanel>

=====================================

Code Behind

protectedvoid Timer1_Tick1(object sender,EventArgs e)

{

GridView1.DataBind(); //needed, or no data update

Label1.Text ="UpdatePanel1 refreshed at: " +DateTime.Now.ToLongTimeString();

UpdatePanel1.Update();

}

You need to setChildrenAsTriggers="false"when you want to callasp:UpdatePanel.Update()inthe timer Tick event handler.It should work fine.
Try to take a look at this document for details when calling asp:UpdatePanel.Update()method -http://ajax.asp.net/docs/mref/M_System_Web_UI_UpdatePanel_Update.aspx.
Wish this can help you.
Thanks Jasson that worked. But you didnt say why it worked in IE7 but not in Mobile 5. I made some further changes in the project
and never could get it working again even with my new knowledge from the documentation.
NOW I have the same problem using a timeras a child. It works in IE7 but a breakpoint in the timer handler
is never reached by either the PDA emulator or a real PDA ... that is in the case that I connect
to the running Visual Studio project. Same behaviour when I publish the project to IIS and connect there.
Why does it work in IE7 and not in IEMobile?
 
protectedvoid Timer1_Tick(object sender,EventArgs e)

{

GridView1.DataBind();

Label1.Text = System.

DateTime.Now.ToString();

}

==========================================================

<%

@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server"><title>Untitled Page</title>

</

head>

<

body><formid="form1"runat="server">
<asp:ScriptManagerID="ScriptManager1"runat="server"/><br/><asp:UpdatePanelID="UpdatePanel1"runat="server"ChildrenAsTriggers="true">
<ContentTemplate>
<asp:TimerID="Timer1"runat="server"Interval="5000"OnTick="Timer1_Tick"></asp:Timer>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NautilusDBConnectionString %>"SelectCommand="SELECT [Source], [Loads] FROM [Loads] ORDER BY [IDnum]"CacheDuration="1"></asp:SqlDataSource>

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataSourceID="SqlDataSource1"Style="z-index: 100; left: 270px; position: absolute; top: 39px"></asp:GridView>

<asp:LabelID="Label1"runat="server"Height="19px"Style="z-index: 102; left: 18px;

position: absolute; top: 186px"

Text="Label"Width="198px"></asp:Label><br/></ContentTemplate></asp:UpdatePanel></form>

</

body>

</

html>

Saturday, March 24, 2012

Ajax Timer - call client JavaScript

Hi,

how can I call a client JavaScript function on ASP.NET AJAX timer tick?

Also, how can I trigger a trigger in an UpdatePanel within a JavaScript function?

Thanks,

Daikoku

Hello,

Find out the event ASP.NET AJAX timer tick ,& the way by which u can subscribe this javascript function to this event

Swati


Sorry, but I'm not following. Can you give a quick example of what you mean?

Thanks!


Hi,

following article will help you

http://www.developerfusion.co.uk/show/5340/,

otherwise post your code , we will find out the other way

Swati


Hi,

It seems that Timer doesn't provide tick event on client. But according to client life cycle, you can use Sys.WebForms.PageRequestManager class' s endRequest event to simulate it.

Please try this:

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> </script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <script type="text/javascript"> function endRequestHandler(sender, args) { yourFunction(); } function yourFunction() { alert("aaa"); } function pageLoad() { if(!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler); } </script> <asp:UpdatePanel ID="UpdatePanel2" runat="server"> <ContentTemplate> <asp:Timer ID="Timer1" runat="server" Interval="6000"> </asp:Timer> </ContentTemplate> </asp:UpdatePanel> </form></body></html>
Hope this helps.

Hi Raymond,

Is there a way to stop / start the timer from the client side script ? also it seems, i cannot abort the timer post back ?

Thx,

Gopi


It's MUCH easier to just use the Sys.Preview.UI.Timer object.

var t = new Sys.Preview.UI.Timer();
t.add_tick(myFunction);

you can use t.stop() and t.play() to control whether the timer fires or not.


Thanks Paul,

I cannot find any documentation for Sys.Preview.UI.Timer() [in ajax.asp.net] ? I want to know how set the timer interval , plus how to dispose the object etc..

Thanks,

Gopi


The second question : How to trigger a trigger from javascript:

I have a button inside the UpdatePanel. The button has UseSubmitBehavior=False (because with True, it does not catch its event handler, when running in FireFox). I have following JS function to trigger the button :

function myTrigger(myId) {
var hidRefresh = document.getElementById("<%=hidRefreshIMG.ClientID%>");
var hidField = document.getElementById("<%=Hidden1.ClientID%>");
hidField.value=myId;
hidRefresh.click(); }

The hidField-thing is to pass an argument back to the server.

The above may not be very beautiful but it works - I found the solution in this posting:

http://forums.asp.net/t/1114574.aspx


It's in the ASP.Net Futures release. iirc changing the timer interval is as simple as t.set_interval(number) where t is your timer instance. You can also just use window.setTimeout() if you're just tryign to call another function and you're not too worried about scoping issues.


oh! ok, future (May 2007) release has some framework upgrade plus other component install, so we cannot consider this solution at this point. Anyhow, thanks for your help.

If anyone know how to start /stop the current version [ajax ver 1.0] of Timer from the client side, that would be great !

Thx,

Gopi


Have you tried calling _startTimer() method? Though it's considered to be private according to the naming convention.

Like this:

var timer = $find("Timer1");

_startTimer();


u mean timer._startTimer() right ? yes, tried that, $find('Timer') returns an object but timer._startTimer exception out, saying no method or property !


<%@. Page Language="C#" AutoEventWireup="true" CodeFile="SetTimerInterval.aspx.cs" Inherits="SetTimerInterval" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"><asp:scriptmanager runat="server"></asp:scriptmanager> <asp:Timer ID="Timer1" runat="server" Interval="3000"> </asp:Timer> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> </asp:UpdatePanel>   <input id="Button1" type="button" value="ChangeInterval" onclick="setTimer();"/> <input id="Button2" type="button" value="Start" onclick="startTimer();"/> <input id="Button3" type="button" value="Stop" onclick="stopTimer();"/> <textarea id="TraceConsole"></textarea> <script type="text/javascript"> function setTimer() { var timer = $find("Timer1"); Sys.Debug.trace(timer.get_interval()); timer.set_interval(100); Sys.Debug.trace(timer.get_interval()); } function startTimer() { var timer = $find("Timer1"); timer._startTimer(); } function stopTimer() { var timer = $find("Timer1"); timer._stopTimer(); } </script> </form></body></html>

Thanks Raymond...

But sometimes I have also faced the problem like

Sys.WebForms.PageRequestMangerServerErrorException: An unkown error occured while processing the request on the server. The status code returned from the server was:500

Any solution for this..

Wednesday, March 21, 2012

AJAX TabPanels and user defined controls

Hi,

I've built a control in which I implement the behaviour described in this video:How Do I: Trigger an UpdatePanel Refresh from a DropDownList Control? Basically, I want to update the nodes of a TreeView when I change the item in a DropDownList.

This works great except if I load the control dynamically into a tab panel...In this case, the first SelectedIndexChanged event doesn't trigger anything and all of the following events trigger a full page postback...

So, to sum this up:

    User control statically defined in a page -> OKUser control statically defined in a tab panel -> OK (it works when I put the code of the control inside the tab panel)User control dynamically loaded into a placeholder -> OK (using PlaceHolder1.Controls.Add(Page.LoadControl("WebUserControl.ascx"));)User control dynamically loaded into a tab panel -> NOT OK! (using both TabPanel1.Controls.Add(Page.LoadControl("WebUserControl.ascx")); and TabPanel1.ContentTemplate = Page.LoadTemplate("WebUserControl.ascx");)

I can't put the code of the control inside the tab panel statically (point 2 in the previous list) because the tab panels are being generated dynamically also...

Anyone has suggestions to solve this, please??

When are you adding these dynamically created controls? It sould be during the Page_Init event and not later (such as during the Page_Load).


I'm not adding the controls in the Page_Init as I can't do that...when my page first loads it has only a DropDownList with a list of options.

Then, when the selected option is changed, there is a postback (as the DropDownList has AutoPostBack = "True") and only then the tabs are created and the user control is loaded.

The user defined control is being rendered and is visible, but its behaviour is strange...


If you dynamically add your contorls into an updatepanel, the issue comes.

There are few good articles written byScott Mitchell on dynamically loading Controls which you will find in the following links:

Dynamic Controls in ASP.NETWorking with Dynamically Created ControlsDynamic Web Controls, Postbacks, and View State