NullReferenceExcepton in Ajax/Atlas UpdatePanel

  • Thread starter =?ISO-8859-15?Q?Tobias_Schr=F6er?=
  • Start date
?

=?ISO-8859-15?Q?Tobias_Schr=F6er?=

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
 
?

=?ISO-8859-15?Q?Tobias_Schr=F6er?=

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top