Showing posts with label ASP.NET AJAX. Show all posts
Showing posts with label ASP.NET AJAX. Show all posts

Thursday, June 19, 2008

How to avoid the javascript alert on error when using UpdatePanel with Timer

By default on error during async request UpdatePanel shows an annoying alert messagebox. There is a way to avoid that and handle errors on your own. Here is how to handle errors in ASP.NET AJAX (This is not only for UpdatePanels). The trick is to add handler to endRequest to the PageRequestManager:

<script type="text/javascript">
function EndRequestHandler( sender, args )
{
if ( args.get_error() != undefined )
{
// set error is handled in order to avoid the messagebox
args.set_errorHandled( true );
}
}

function AppLoad()
{
Sys.WebForms.PageRequestManager.getInstance().add_endRequest( EndRequestHandler );
}

Sys.Application.add_load( AppLoad );
</script>

Tuesday, June 17, 2008

UpdatePanel doing full postback when it should not!

I had an UpdatePanel with a Timer for AsyncPostBackTrigger. But although my ScriptManager had EnablePartialRendering="true", my page always got refreshed. The problem was a setting in my web.config. I removed this line:

<xhtmlConformance mode="Legacy"/>

This setting was something that probably I forgot to remove when migrating from asp.net 1.1 to asp.net 2.0.

Tuesday, March 18, 2008

Keeping Session Alive With ASP.NET AJAX

I needed to keep session alive as long as a customer is viewing a page, no matter if he/she clicks somewhere on the page. With asp.net ajax, this was very easy.

First, you need a ScriptManager on your page with EnablePageMethods set to true:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
</form>


Second, define in your code behind a simple method that does nothing but keeps session alive like this.

[WebMethod]
[ScriptMethod]
public static void KeepAlive()
{
// just keep session alive
}


Note that you have to use WebMethod and ScriptMethod attributes to make this method avaliable for using in scripts.

Third, you have to define javascript methods to call a page method that is going to keep your session alive.

<script type="text/javascript">
function RegisterKeepAlive( sec )

{
setInterval( "PageMethods.KeepAlive( OnComplete, OnTimeOut, OnError )", sec*1000 );
}
function OnComplete( arg ) {}
function OnTimeOut( arg ){}
function OnError( arg ) {}
</script>


Last, just call RegisterKeepAlive with for example 10 seconds on load event of the body:

<body onload="RegisterKeepAlive( 10 )">

You can also use UpdatePanel with Timer control, but this way is better, because you have more options for processing timeout and errors.

Monday, March 17, 2008

Migrating to VS2005 Web Application Project Problems

When I was trying to migrate to my VS2005 Web Site Project to VS2005 Web Application Project, I had a weird problem. I had one user control that uses asp.net ajax and a page that uses this control. While the control itself succeeded in Converto To Web Application operation (from the context menu of the project), the page did not. What solved the problem was adding a Register directive to the page (just for generating the designer file) and removing it after that:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>

Another thing that took me some time was that I had to change manually the Build Action for all my files from App_Code from Content to Compile.

More for migrating from vs2005 web site project to vs2005 web app project can find here. If you want to migrate vs2003 web project to vs2005 web app project, see here.

Sunday, November 11, 2007

PopupControlExtender To Show On MouseOver

This is an example of how to do it. Set unique BehaviorID to PopupControlExtender and call showPopup/hidePopup with it:

<ajaxToolkit:PopupControlExtender ID="PopupControlExtender1" runat="server" BehaviorID="pce" ............... />

<asp:Panel onMouseOver="$find('pce').showPopup();" onMouseOut="$find('pce').hidePopup();" />


NOTE: If you use it in a user control that could be used more than once on a page - give unique BehaviorID to each one.

Monday, November 5, 2007

FileUpload Control Within An UpdatePanel

This will work by forcing a full postback. In order to do this, we use PostBackTrigger:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
<ContentTemplate>
<ews:DatePicker ID="DatePicker1" runat="server" UsingUpdatePanel="True" OnSelectionChanged="DatePicker1_SelectionChanged" /><br />
<asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
<asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="Button1" runat="server"
Text="Upload" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>

See: Triggers - a better control over postback , FileUpload example

Thursday, November 1, 2007

Don't use <xhtmlconformance mode="Legacy"/> with ASP.NET AJAX

I do not have time to research why ASP.NET AJAX Toolkit is not working with <xhtmlconformance mode="Legacy"/> in web.config file, but anyway, hope I could help someone having my problem...

I had a calendar extender, but I got the error (as a javascript error):
this._pagerequestManager is null or not an object

The problem was that I used YAF component (YetAnotherForum.net) which uses this mode:
xhtmlconformance mode="Legacy"

PS: Hah, what I just understood is that the difference between mode="Legacy" and mode="Transactional" (default) is the forming of UniqueID of the controls (UniqueID property includes the identifier for the server control's naming container... more). For example, if you do not name a control, its ClientID is ctl0 or ctl00 depending on the mode mentioned above. If that control contains child controls, they would have the prefix ctl0 ot ctl00 as well.

See xhtmlConformance Element in MSDN.
See more about this.
eXTReMe Tracker