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

Discussion in 'ASP .Net' started by Michael Schöller, Nov 20, 2009.

  1. 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
     
    Michael Schöller, Nov 20, 2009
    #1
    1. Advertising

  2. 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
     
    Michael Schöller, Nov 23, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    493
    Rob Meade
    Aug 8, 2007
  2. ma
    Replies:
    3
    Views:
    547
  3. Arachnid
    Replies:
    0
    Views:
    787
    Arachnid
    Oct 5, 2007
  4. Mel
    Replies:
    1
    Views:
    4,098
  5. javelin
    Replies:
    22
    Views:
    397
    Thomas 'PointedEars' Lahn
    Oct 8, 2007
Loading...

Share This Page