Showing posts with label updates. Show all posts
Showing posts with label updates. Show all posts

Saturday, March 24, 2012

Ajax timer update does not work when it updates a text box with some special characters

The Ajax time seems to have some trouble updating a panel that contains a text box with some special characters. I managed to replicate the problem using a very simple page that is inlcuded below. The problem is that the second update causes an error 500 from the development server of Visual Studio.

The page just has an update panel with a single multi-line text box. The code for the timer update assignes the text box the string "<br>\n".

Does anyone know how to solve this problem?

The Page is:

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.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 ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick"> </asp:Timer> </ContentTemplate> </asp:UpdatePanel> </form></body></html>
The code for the update is:

public partialclass _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e) { }protected void Timer1_Tick(object sender, EventArgs e) { TextBox1.Text = "<br>\n;"; }}

Regards,

Ivan

By the way, I do realize that the problem is with special characters that trigger the ASP.NET security validations. Changing the text in the Text Box to HTML encoded is not an option, because in this text box the user should be entering and editing HTML code.

AJAX Timer bug when it updates a panel containing a single Text Box with the characters "&

The Ajax timer seems to have a problem updating a panel what contains a text box that has the characters "<br>\n". Attached is the source code for a very simple page a single multi-line text box where the timer code-behind just assigns the textbox the string "<br>\n". When run, the second update will cause an 500 error from the server.

This error does not happen when the text is something else, say "Hello World".

I assume that the problem is that the sequence "<br>\n" in the text box causes the timer Java Script to break somehow. Anyone knows oh to solve this problem? I am using Ajax 1.0 and the Visual Studio Development Server.

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.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 ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer1_Tick"> </asp:Timer> </ContentTemplate> </asp:UpdatePanel> </form></body></html>

The code behind for the time update is:

protected void Timer1_Tick(object sender, EventArgs e) { TextBox1.Text ="<br>\n;"; }

Ivan.

I am sorry, the code for the post got mangled but the editor. The code behind for the time update should be:

public partialclass _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e) { }protected void Timer1_Tick(object sender, EventArgs e) { TextBox1.Text = "<br>\n"; }}

Apparently, the string <br> is detected as a potentially harmfull string. One solution is to HTML encode the string. However, I do need to display the string <br> in the text box, not <br>. The user must be able to edit the HTML code that we will use.

Ivan.


Apparently, the string <br> is detected as a potentially harmfull string. One solution is to HTML encode the string. However, I do need to display the string <br> in the text box, not <br>. The user must be able to edit the HTML code that we will use.

Ivan.

AJAX timer and updatepanel updates entire page!

Hi,

Below is a sample of a very simple page:

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Data_test" Title="Untitled Page" %>
<%@dotnet.itags.org. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%@dotnet.itags.org. Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Timers</title>
</head>
<body>sdfsdf
<form id="form1" runat="server">sdsdf
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick" />
<asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label runat="server" id="DateStampLabel" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

and the codebehind:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using AjaxControlToolkit;

public partial class Data_test : BasePage
{

protected override void Page_Load(object sender, EventArgs e)
{
// Run common functionality in Base page
base.Page_Load(sender, e);

if (User.Identity.IsAuthenticated == false)
{
Server.Transfer("~/Default.aspx");
}

Page.MaintainScrollPositionOnPostBack = true;
}

protected void UpdateTimer_Tick(object sender, EventArgs e)
{
DateStampLabel.Text = DateTime.Now.ToString();
}

}

When I run this, the whole page is refreshed, not just the label in the updatepanel!!!

Why is this??

Thanks,

Kreid

Hey,

Just to be safe, try adding EnablePartialRendering="true" to the script manager. Also, you may want to try putting the timer within the update panel, and see if that is the issue.


Thanks for your reply.

EnablePartialRendering was set to to true. I also just tried to put the timer inside the updatepanel, and set updatemode = Always. No change - the whole page is still updated.

:( Any more ideas?

bmains:

Hey,

Just to be safe, try adding EnablePartialRendering="true" to the script manager. Also, you may want to try putting the timer within the update panel, and see if that is the issue.


Keep in mind the code in your Page_Load is going to be executed in partial postbacks. Wrap that Page_Load code in if (!IsPostBack) { }, and it should work as expected.