Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

NullReferenceExcepton in Ajax/Atlas UpdatePanel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tobias Schröer - 20 Aug 2007 16:55 GMT
Hi,

I have a web site with an UpdatePanel, a Label within that panel and a
Button outside the panel. All controls are created programatically (see
code below).
When I click the button to update the label's text, I get a
NullReferenceException in the page's Render method. My own code runs
without any problems.

Any one with some ideas? I already did some Google reseach but came out
with nothing that would help be :(

<code>
public partial class test : System.Web.UI.Page {

  UpdatePanel up;
  Label l1;
  Panel p;
  UpdateProgress upp;
  Button b;
  UpdateProgressOverlayExtender upoe;

  protected override void OnPreInit(EventArgs e) {
    base.OnPreInit(e);
    // create dynamic controls
    up = new UpdatePanel();

    p = new Panel();
    up.ContentTemplateContainer.Controls.Add(p);

    l1 = new Label();
    p.Controls.Add(l1);

    this.form1.Controls.Add(up);
     
    b = new Button();
    this.form1.Controls.Add(b);
  }

  protected override void OnLoad(EventArgs e) {
    base.OnLoad(e);

    // control properties
    l1.ID = "l1";

    // some backgroud
    p.Style.Value =
      "width:100%; height:100px; text-align:center; " +
      "background:#3366a4; border:solid 1px black;";

    up.ID = "up";
    up.ChildrenAsTriggers = false;
    up.UpdateMode = UpdatePanelUpdateMode.Conditional;

    b.Text = "Update Panel";
    b.ID = "b";
    b.Click += new EventHandler(this.ButtonClick);

    // add button as update trigger
    AsyncPostBackTrigger apt = new AsyncPostBackTrigger();
    apt.ControlID = b.ClientID;
    up.Triggers.Add(apt);

    this.ScriptManager1.RegisterAsyncPostBackControl(b);
  }

  protected void ButtonClick(object sender, EventArgs e) {
    // wait a litte
    Thread.Sleep(2000);
    // set label text
    l1.Text = "Panel refreshed at " + DateTime.Now.ToString();
  }

  protected override void Render(HtmlTextWriter writer) {
    try {
      // here the error occurs
      base.Render(writer);
    } catch (Exception ex) {
      writer.Write(ex.ToString());
    }
  }
}
</code>

Thanks,
Tobi
Tobias Schröer - 21 Aug 2007 11:49 GMT
Hi,

just in case someone is interested:

I think I have solved the problem. The AsyncPostBackTrigger did not
re-initialize on postback. Therefore, the associaed control was not set
properly, which caused the exception.

As a solution, I derived a class MyUpdatePanel from the original
UpdatePanel class and overrode the OnLoad method. Here I ensured the
initialization on postbacks explicitly. In the base implementation this
is not done. I wonder if its either a bug or by intention. Any
suggestions here? So far I had no drawbacks.

<code>
public class MyUpdatePanel : UpdatePanel {
  protected override void OnLoad(EventArgs e) {
    // explicit initialize on postback
    if (this.Page.IsPostBack) {
      this.Initialize();
    }
    base.OnLoad(e);
  }
}
</code>

Tobias Schröer schrieb:
> Hi,
>
[quoted text clipped - 82 lines]
> Thanks,
> Tobi

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.