PC Review


Reply
Thread Tools Rate Thread

AJAX UpdatePanel does not refresh after calling Update()-Method

 
 
Michael Schöller
Guest
Posts: n/a
 
      20th Nov 2009
Hi,

I'm new to AJAX and has a little Problem to get an UpdatePanel inside an
CustomServerControl to work.
My goal is to create an CustomServerControl with an (at designtime)
unknown number of UpdatePanels, that can be updated seperatly.
Since I'm stuck I create a litte more static testproject to analyse the
problem but I ran out of ideas what I could been missing.
I would be realy glad if someone can show me hwo to get the UpdatePanels
working.

I created an ServerControl-Project called ServerControlTest and an ASP
WebApplication called AXAJTest
The Default.asxp looks like:
--------------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="AJAXTest._Default" %>

<%@ Register assembly="ServerControlTest" namespace="ServerControlTest"
tagprefix="cc1" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000"
ontick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

</div>
<cc1:ServerControl1 ID="ServerControl11" runat="server" />
</form>
</body>
</html>
---------------------
Code Behind looks like:
------------------------------------------------------
using System;

namespace AJAXTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ServerControl11.Text1 = DateTime.Now.ToString();
ServerControl11.Text2 = DateTime.Now.AddDays(1D).ToString();
}
}

protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
ServerControl11.Text1 = DateTime.Now.ToString();
ServerControl11.Text2 = DateTime.Now.AddDays(1D).ToString();

ServerControl11.TestPanel1.Update();
}
}
}
-----------------------------------------------
The class ServerControl1 in ServerControlTest looks like this:
----------------------------------------------------------
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServerControlTest
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class ServerControl1 : WebControl
{
private UpdatePanel _testPanel1;
private UpdatePanel _testPanel2;

public UpdatePanel TestPanel1 { get { return _testPanel1; } }
public UpdatePanel TestPanel2 { get { return _testPanel2; } }

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text1
{
get
{
String s = (String)ViewState["Text1"];
return (s ?? string.Empty);
}

set
{
ViewState["Text1"] = value;
}
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text2
{
get
{
String s = (String)ViewState["Text1"];
return (s ?? string.Empty);
}

set
{
ViewState["Text2"] = value;
}
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_testPanel1 = new UpdatePanel
{
ID = this.ID + this.IdSeparator + "UpdatePanel1",
UpdateMode = UpdatePanelUpdateMode.Conditional,
ChildrenAsTriggers = false,
Page = this.Page
};
_testPanel2 = new UpdatePanel
{
ID = this.ID + this.IdSeparator + "UpdatePanel2",
UpdateMode = UpdatePanelUpdateMode.Conditional,
ChildrenAsTriggers = false,
Page = this.Page
};
}

protected override void RenderContents(HtmlTextWriter output)
{
Label label1 = new Label {ID = this.ID + this.IdSeparator +
"Label1", Text = Text1};
Label label2 = new Label {ID = this.ID + this.IdSeparator +
"Label2", Text = Text2};
_testPanel1.ContentTemplateContainer.Controls.Add(label1);
_testPanel2.ContentTemplateContainer.Controls.Add(label2);
_testPanel1.RenderControl(output);
_testPanel2.RenderControl(output);
}
}
}
---------------------------------------------------

Regards
Michael
 
Reply With Quote
 
 
 
 
Michael Schöller
Guest
Posts: n/a
 
      23rd Nov 2009
Hi,

I found a solution myself.
There are 3 things that must be changed.
1) The UpdatePanels has to be declares in the PageLoad-Event and added
to the Controls-Container.
2) The Label-Controls has to be declared in the PageLoad-Event and added
to the updatePanel
3) The Text-Property of the Label Control has to be set in the
OnPreRenderContent-Event.

After that changes all works fine.

Regards
Michael

Michael Schöller schrieb:
> Hi,
>
> I'm new to AJAX and has a little Problem to get an UpdatePanel inside
> an CustomServerControl to work.
> My goal is to create an CustomServerControl with an (at designtime)
> unknown number of UpdatePanels, that can be updated seperatly.
> Since I'm stuck I create a litte more static testproject to analyse
> the problem but I ran out of ideas what I could been missing.
> I would be realy glad if someone can show me hwo to get the
> UpdatePanels working.
>
> I created an ServerControl-Project called ServerControlTest and an ASP
> WebApplication called AXAJTest
> The Default.asxp looks like:
> --------------------------
> <%@ Page Language="C#" AutoEventWireup="true"
> CodeBehind="Default.aspx.cs" Inherits="AJAXTest._Default" %>
>
> <%@ Register assembly="ServerControlTest"
> namespace="ServerControlTest" tagprefix="cc1" %>
>
> <!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></title>
> </head>
> <body>
> <form id="form1" runat="server">
> <asp:ScriptManager ID="ScriptManager1" runat="server">
> </asp:ScriptManager>
> <div>
> <asp:UpdatePanel ID="UpdatePanel1" runat="server">
> <ContentTemplate>
> <asp:Timer ID="Timer1" runat="server" Interval="1000"
> ontick="Timer1_Tick">
> </asp:Timer>
> <asp:Label ID="Label1" runat="server"
> Text="Label"></asp:Label>
> </ContentTemplate>
> </asp:UpdatePanel>
> </div>
> <cc1:ServerControl1 ID="ServerControl11" runat="server" />
> </form>
> </body>
> </html>
> ---------------------
> Code Behind looks like:
> ------------------------------------------------------
> using System;
>
> namespace AJAXTest
> {
> public partial class _Default : System.Web.UI.Page
> {
> protected void Page_Load(object sender, EventArgs e)
> {
> if(!IsPostBack)
> {
> ServerControl11.Text1 = DateTime.Now.ToString();
> ServerControl11.Text2 =
> DateTime.Now.AddDays(1D).ToString();
> }
> }
>
> protected void Timer1_Tick(object sender, EventArgs e)
> {
> Label1.Text = DateTime.Now.ToString();
> ServerControl11.Text1 = DateTime.Now.ToString();
> ServerControl11.Text2 = DateTime.Now.AddDays(1D).ToString();
>
> ServerControl11.TestPanel1.Update();
> }
> }
> }
> -----------------------------------------------
> The class ServerControl1 in ServerControlTest looks like this:
> ----------------------------------------------------------
> using System;
> using System.ComponentModel;
> using System.Web.UI;
> using System.Web.UI.WebControls;
>
> namespace ServerControlTest
> {
> [DefaultProperty("Text")]
> [ToolboxData("<{0}:ServerControl1
> runat=server></{0}:ServerControl1>")]
> public class ServerControl1 : WebControl
> {
> private UpdatePanel _testPanel1;
> private UpdatePanel _testPanel2;
>
> public UpdatePanel TestPanel1 { get { return _testPanel1; } }
> public UpdatePanel TestPanel2 { get { return _testPanel2; } }
>
> [Bindable(true)]
> [Category("Appearance")]
> [DefaultValue("")]
> [Localizable(true)]
> public string Text1
> {
> get
> {
> String s = (String)ViewState["Text1"];
> return (s ?? string.Empty);
> }
>
> set
> {
> ViewState["Text1"] = value;
> }
> }
>
> [Bindable(true)]
> [Category("Appearance")]
> [DefaultValue("")]
> [Localizable(true)]
> public string Text2
> {
> get
> {
> String s = (String)ViewState["Text1"];
> return (s ?? string.Empty);
> }
>
> set
> {
> ViewState["Text2"] = value;
> }
> }
>
> protected override void OnInit(EventArgs e)
> {
> base.OnInit(e);
> _testPanel1 = new UpdatePanel
> {
> ID = this.ID + this.IdSeparator + "UpdatePanel1",
> UpdateMode = UpdatePanelUpdateMode.Conditional,
> ChildrenAsTriggers = false,
> Page = this.Page
> };
> _testPanel2 = new UpdatePanel
> {
> ID = this.ID + this.IdSeparator + "UpdatePanel2",
> UpdateMode = UpdatePanelUpdateMode.Conditional,
> ChildrenAsTriggers = false,
> Page = this.Page
> };
> }
>
> protected override void RenderContents(HtmlTextWriter output)
> {
> Label label1 = new Label {ID = this.ID + this.IdSeparator +
> "Label1", Text = Text1};
> Label label2 = new Label {ID = this.ID + this.IdSeparator +
> "Label2", Text = Text2};
> _testPanel1.ContentTemplateContainer.Controls.Add(label1);
> _testPanel2.ContentTemplateContainer.Controls.Add(label2);
> _testPanel1.RenderControl(output);
> _testPanel2.RenderControl(output);
> }
> }
> }
> ---------------------------------------------------
>
> Regards
> Michael

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ajax Update the UpdatePanel every 5 sec pvong Microsoft ASP .NET 1 28th Oct 2009 07:25 PM
The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' before Render.| Jeff Microsoft ASP .NET 0 8th Mar 2008 01:13 PM
AJAX using UpdatePanel and without it in the same code Oleg Microsoft ASP .NET 1 4th Mar 2008 09:48 AM
updating an update panel from another updatepanel (AJAX) ma Microsoft ASP .NET 3 29th Sep 2007 11:04 PM
Help with AJAX updatepanel? brett Microsoft ASP .NET 1 4th Feb 2007 03:26 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:03 AM.