|
|
|
start date: Mon, 20 Aug 2007 17:55:31 +0200,
posted on: microsoft.public.dotnet.framework.aspnet
back
| Thread Index |
|
1
Tobias Schröer
|
|
2
Tobias Schröer
|
NullReferenceExcepton in Ajax/Atlas UpdatePanel
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
Date:Mon, 20 Aug 2007 17:55:31 +0200
Author:
|
Re: NullReferenceExcepton in Ajax/Atlas UpdatePanel
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 Schrer schrieb:
> 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
Date:Tue, 21 Aug 2007 12:49:42 +0200
Author:
|
|
|