J
Jonathan Wood
I'm designing a control that will render some additional HTML content
exactly once per page, regardless of the number of instances of the control
on that page.
Thanks to Mr. Niver's suggestions, I got this working by calling
Page.ClientScript.IsStartupScriptRegistered() from my control's Load event
and writing that additional HTML content from the control only when
IsStartupScriptRegistered() returns false.
This seemed to be working just fine from a Web User Control. But I'm now
moving my control to a regular Web control. The first problem is that the
Control has no Load event. One of my books shows a control initializing
itself in the OnInit() override so I tried my code there:
protected override void OnInit(EventArgs e)
{
// ...
_createHiddenField = false;
if (!Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"OrderCartRequest", "function dummy() {}", true);
_createHiddenField = true;
}
// ...
}
I placed five instances of this control on a page and set a breakpoint in
the code above. I see that Page.ClientScript.IsStartupScriptRegistered()
returns false when it is called each of the five times from each instance of
my control. Obviously, I expect false to be returned only for the first
instance of the control.
Can anyone see what I'm missing?
Thanks.
Jonathan
exactly once per page, regardless of the number of instances of the control
on that page.
Thanks to Mr. Niver's suggestions, I got this working by calling
Page.ClientScript.IsStartupScriptRegistered() from my control's Load event
and writing that additional HTML content from the control only when
IsStartupScriptRegistered() returns false.
This seemed to be working just fine from a Web User Control. But I'm now
moving my control to a regular Web control. The first problem is that the
Control has no Load event. One of my books shows a control initializing
itself in the OnInit() override so I tried my code there:
protected override void OnInit(EventArgs e)
{
// ...
_createHiddenField = false;
if (!Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"OrderCartRequest", "function dummy() {}", true);
_createHiddenField = true;
}
// ...
}
I placed five instances of this control on a page and set a breakpoint in
the code above. I see that Page.ClientScript.IsStartupScriptRegistered()
returns false when it is called each of the five times from each instance of
my control. Obviously, I expect false to be returned only for the first
instance of the control.
Can anyone see what I'm missing?
Thanks.
Jonathan