Main Page | Report Page

 

  .NET DotNet Forum Index » ASP.NET Forum » ClientScript.IsStartupScriptRegistered() from Web...

Author Message
Jonathan Wood...
Posted: Mon May 04, 2009 7:26 pm
 
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
 
William Niver...
Posted: Mon May 04, 2009 7:38 pm
 
So you went from an *.ascx file to a compiled server control?

If so, pop yer code into the "Protected Overrides Sub CreateChildControls()"
Method

Sample:

Protected Overrides Sub CreateChildControls()
MyBase.Controls.Clear()
Dim Div_Main As New HtmlGenericControl("div")
MyBase.Controls.Add(Div_Main)
RenderHiddenField(Div_Main)
End Sub

Private Sub RenderHiddenField(ByRef container As Control)
'check for registration here. If not registered, add it to the
container (which is Div_Main)
Dim hdn_Field As New HiddenField
hdn_Field.ID = "hdn_Field"
container.Controls.Add(hdn_Field)
End Sub

Hope that helps!

William


"Jonathan Wood" <jwood at (no spam) softcircuits.com> wrote in message
news:u3GDDCSzJHA.1372 at (no spam) TK2MSFTNGP05.phx.gbl...
Quote:
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

 
Jonathan Wood...
Posted: Mon May 04, 2009 9:32 pm
 
Hi William,

Quote:
So you went from an *.ascx file to a compiled server control?

Yup. I'm liking the separate assemblies. Reminds me more of the olden
days... <g>

Quote:
If so, pop yer code into the "Protected Overrides Sub
CreateChildControls()" Method

Well, I had a few questions about why you were doing it this way but I still
must be doing something wrong as I didn't even get that far.
ClientScript.IsStartupScriptRegistered() is still returning false for each
control and a hidden field is being created for each control.

I don't know if I'm missing something, or if ClientScript just doesn't work
from this location...

protected override void CreateChildControls()
{
// base.CreateChildControls();

Controls.Clear();
HtmlGenericControl div = new HtmlGenericControl("div");
Controls.Add(div);
RenderHiddenField(div);
}

protected void RenderHiddenField(Control div)
{
// Encrypt our view state for added security
// Page.RegisterRequiresViewStateEncryption();

if (!Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"OrderCartRequest", "function dummy() {}", true);

HiddenField hidden = new HiddenField();
hidden.ID = "OrderCartRequest";
div.Controls.Add(hidden);
}
}

Thanks.

Jonathan
 
William Niver...
Posted: Mon May 04, 2009 9:51 pm
 
try:

If (Not Me.Page.ClientScript.IsStartupScriptRegistered(Me.Page.GetType,
"OrderCartRequest")) Then

instead of:

If (Not Me.Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest"))
Then

Take care!

William

PS

I'm not a programmer... but I did stay at a Holiday Inn Express last night!
*grin*

"Jonathan Wood" <jwood at (no spam) softcircuits.com> wrote in message
news:eZEwaITzJHA.1416 at (no spam) TK2MSFTNGP04.phx.gbl...
Quote:
Hi William,

So you went from an *.ascx file to a compiled server control?

Yup. I'm liking the separate assemblies. Reminds me more of the olden
days... <g

If so, pop yer code into the "Protected Overrides Sub
CreateChildControls()" Method

Well, I had a few questions about why you were doing it this way but I
still must be doing something wrong as I didn't even get that far.
ClientScript.IsStartupScriptRegistered() is still returning false for each
control and a hidden field is being created for each control.

I don't know if I'm missing something, or if ClientScript just doesn't
work from this location...

protected override void CreateChildControls()
{
// base.CreateChildControls();

Controls.Clear();
HtmlGenericControl div = new HtmlGenericControl("div");
Controls.Add(div);
RenderHiddenField(div);
}

protected void RenderHiddenField(Control div)
{
// Encrypt our view state for added security
// Page.RegisterRequiresViewStateEncryption();

if (!Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"OrderCartRequest", "function dummy() {}", true);

HiddenField hidden = new HiddenField();
hidden.ID = "OrderCartRequest";
div.Controls.Add(hidden);
}
}

Thanks.

Jonathan

 
Jonathan Wood...
Posted: Mon May 04, 2009 10:01 pm
 
That was it! Although, I really can't say why.

I'll need to tweak this now that I'm back to reality. If you don't mind, I
had a couple of questions about your last code.

* Why are you clearing the control collection?
* Does it matter that you aren't calling base.CreateChildControls()?
* Why do you need the containing div? Why not just add it to the control?

Other than that, you've been a terrific help. I've obviously got a lot more
reading to do but I've learned a lot over the past week and you've helped me
move forward!

Jonathan

"William Niver" <thor.niver at (no spam) sbcglobal.net> wrote in message
news:OjYoKTTzJHA.5728 at (no spam) TK2MSFTNGP03.phx.gbl...
Quote:
try:

If (Not Me.Page.ClientScript.IsStartupScriptRegistered(Me.Page.GetType,
"OrderCartRequest")) Then

instead of:

If (Not
Me.Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest")) Then

Take care!

William

PS

I'm not a programmer... but I did stay at a Holiday Inn Express last
night! *grin*

"Jonathan Wood" <jwood at (no spam) softcircuits.com> wrote in message
news:eZEwaITzJHA.1416 at (no spam) TK2MSFTNGP04.phx.gbl...
Hi William,

So you went from an *.ascx file to a compiled server control?

Yup. I'm liking the separate assemblies. Reminds me more of the olden
days... <g

If so, pop yer code into the "Protected Overrides Sub
CreateChildControls()" Method

Well, I had a few questions about why you were doing it this way but I
still must be doing something wrong as I didn't even get that far.
ClientScript.IsStartupScriptRegistered() is still returning false for
each control and a hidden field is being created for each control.

I don't know if I'm missing something, or if ClientScript just doesn't
work from this location...

protected override void CreateChildControls()
{
// base.CreateChildControls();

Controls.Clear();
HtmlGenericControl div = new HtmlGenericControl("div");
Controls.Add(div);
RenderHiddenField(div);
}

protected void RenderHiddenField(Control div)
{
// Encrypt our view state for added security
// Page.RegisterRequiresViewStateEncryption();

if (!Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"OrderCartRequest", "function dummy() {}", true);

HiddenField hidden = new HiddenField();
hidden.ID = "OrderCartRequest";
div.Controls.Add(hidden);
}
}

Thanks.

Jonathan


 
William Niver...
Posted: Mon May 04, 2009 10:44 pm
 
1) Why am I clearing teh control Collection?

Force of habit. It only really becomes an issue when I make extremely
complex controls. There are points when setting properties or returning
events that I'll use a combination of:

ChildControlsCreated = False
EnsureChildControls

To force teh control to remake itself based on those new values. If I
didn't clear the control first, I'd get duplicated controls on the page.
I haven't found an issue with using it on more simple controls, so it stuck
as habit.

If I added a literalcontrol("HelloWorld") in CreateChildControls and then
called CreateChildControls or EnsureChildControls (you shouldn't ever call
CreateChildControls Directly), I'd wind up with two literals that said
"Hello World".

2) Calling MyBase,CreateChildControls()

Yer already *in* MyBase.CreateChildControls *grin*... why would I call it
again?
You'll get that with all the base overrides. If you Override Render, by
default it will pop in MyBase.Render(stuff).
You are overriding because you generally don't want to do the default
things, so ya change them. When I override Render, I generally change it
to:

MyBase.RenderContents(stuff)

3) The Containing Div is not required, it's once again, force of habit.

I often unessessarilly apply lessons I've learned from creating complex
compiled server controls to more simple ones, expecting that they will grow
more complex over time and will benefit from being contructed as if they
will be complex from the start.


"Jonathan Wood" <jwood at (no spam) softcircuits.com> wrote in message
news:OQkp8YTzJHA.1380 at (no spam) TK2MSFTNGP05.phx.gbl...
Quote:
That was it! Although, I really can't say why.

I'll need to tweak this now that I'm back to reality. If you don't mind, I
had a couple of questions about your last code.

* Why are you clearing the control collection?
* Does it matter that you aren't calling base.CreateChildControls()?
* Why do you need the containing div? Why not just add it to the control?

Other than that, you've been a terrific help. I've obviously got a lot
more reading to do but I've learned a lot over the past week and you've
helped me move forward!

Jonathan

"William Niver" <thor.niver at (no spam) sbcglobal.net> wrote in message
news:OjYoKTTzJHA.5728 at (no spam) TK2MSFTNGP03.phx.gbl...
try:

If (Not Me.Page.ClientScript.IsStartupScriptRegistered(Me.Page.GetType,
"OrderCartRequest")) Then

instead of:

If (Not
Me.Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest")) Then

Take care!

William

PS

I'm not a programmer... but I did stay at a Holiday Inn Express last
night! *grin*

"Jonathan Wood" <jwood at (no spam) softcircuits.com> wrote in message
news:eZEwaITzJHA.1416 at (no spam) TK2MSFTNGP04.phx.gbl...
Hi William,

So you went from an *.ascx file to a compiled server control?

Yup. I'm liking the separate assemblies. Reminds me more of the olden
days... <g

If so, pop yer code into the "Protected Overrides Sub
CreateChildControls()" Method

Well, I had a few questions about why you were doing it this way but I
still must be doing something wrong as I didn't even get that far.
ClientScript.IsStartupScriptRegistered() is still returning false for
each control and a hidden field is being created for each control.

I don't know if I'm missing something, or if ClientScript just doesn't
work from this location...

protected override void CreateChildControls()
{
// base.CreateChildControls();

Controls.Clear();
HtmlGenericControl div = new HtmlGenericControl("div");
Controls.Add(div);
RenderHiddenField(div);
}

protected void RenderHiddenField(Control div)
{
// Encrypt our view state for added security
// Page.RegisterRequiresViewStateEncryption();

if (!Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"OrderCartRequest", "function dummy() {}", true);

HiddenField hidden = new HiddenField();
hidden.ID = "OrderCartRequest";
div.Controls.Add(hidden);
}
}

Thanks.

Jonathan



 
Jonathan Wood...
Posted: Mon May 04, 2009 11:39 pm
 
Okay, that's making sense to me now. Now that things are actually working,
I've tweaked the heck out of the original code. I now have a drop-in image
button that automatically creates a hidden field, and when it's clicked, it
writes encrypted product data to that hidden field, before being posted to
the shopping cart page. And my shopping cart page correctly displays the
info, can process a credit card payment, and generate an authorization code
for any of my shareware products!

I still have a lot of work, refining these steps but it's coming along
nicely.

Thanks again for all your help.

Jonathan

"William Niver" <thor.niver at (no spam) sbcglobal.net> wrote in message
news:Ord5AxTzJHA.3872 at (no spam) TK2MSFTNGP02.phx.gbl...
Quote:
1) Why am I clearing teh control Collection?

Force of habit. It only really becomes an issue when I make extremely
complex controls. There are points when setting properties or returning
events that I'll use a combination of:

ChildControlsCreated = False
EnsureChildControls

To force teh control to remake itself based on those new values. If I
didn't clear the control first, I'd get duplicated controls on the page.
I haven't found an issue with using it on more simple controls, so it
stuck as habit.

If I added a literalcontrol("HelloWorld") in CreateChildControls and then
called CreateChildControls or EnsureChildControls (you shouldn't ever call
CreateChildControls Directly), I'd wind up with two literals that said
"Hello World".

2) Calling MyBase,CreateChildControls()

Yer already *in* MyBase.CreateChildControls *grin*... why would I call it
again?
You'll get that with all the base overrides. If you Override Render, by
default it will pop in MyBase.Render(stuff).
You are overriding because you generally don't want to do the default
things, so ya change them. When I override Render, I generally change it
to:

MyBase.RenderContents(stuff)

3) The Containing Div is not required, it's once again, force of habit.

I often unessessarilly apply lessons I've learned from creating complex
compiled server controls to more simple ones, expecting that they will
grow more complex over time and will benefit from being contructed as if
they will be complex from the start.


"Jonathan Wood" <jwood at (no spam) softcircuits.com> wrote in message
news:OQkp8YTzJHA.1380 at (no spam) TK2MSFTNGP05.phx.gbl...
That was it! Although, I really can't say why.

I'll need to tweak this now that I'm back to reality. If you don't mind,
I had a couple of questions about your last code.

* Why are you clearing the control collection?
* Does it matter that you aren't calling base.CreateChildControls()?
* Why do you need the containing div? Why not just add it to the control?

Other than that, you've been a terrific help. I've obviously got a lot
more reading to do but I've learned a lot over the past week and you've
helped me move forward!

Jonathan

"William Niver" <thor.niver at (no spam) sbcglobal.net> wrote in message
news:OjYoKTTzJHA.5728 at (no spam) TK2MSFTNGP03.phx.gbl...
try:

If (Not Me.Page.ClientScript.IsStartupScriptRegistered(Me.Page.GetType,
"OrderCartRequest")) Then

instead of:

If (Not
Me.Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest")) Then

Take care!

William

PS

I'm not a programmer... but I did stay at a Holiday Inn Express last
night! *grin*

"Jonathan Wood" <jwood at (no spam) softcircuits.com> wrote in message
news:eZEwaITzJHA.1416 at (no spam) TK2MSFTNGP04.phx.gbl...
Hi William,

So you went from an *.ascx file to a compiled server control?

Yup. I'm liking the separate assemblies. Reminds me more of the olden
days... <g

If so, pop yer code into the "Protected Overrides Sub
CreateChildControls()" Method

Well, I had a few questions about why you were doing it this way but I
still must be doing something wrong as I didn't even get that far.
ClientScript.IsStartupScriptRegistered() is still returning false for
each control and a hidden field is being created for each control.

I don't know if I'm missing something, or if ClientScript just doesn't
work from this location...

protected override void CreateChildControls()
{
// base.CreateChildControls();

Controls.Clear();
HtmlGenericControl div = new HtmlGenericControl("div");
Controls.Add(div);
RenderHiddenField(div);
}

protected void RenderHiddenField(Control div)
{
// Encrypt our view state for added security
// Page.RegisterRequiresViewStateEncryption();

if (!Page.ClientScript.IsStartupScriptRegistered("OrderCartRequest"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"OrderCartRequest", "function dummy() {}", true);

HiddenField hidden = new HiddenField();
hidden.ID = "OrderCartRequest";
div.Controls.Add(hidden);
}
}

Thanks.

Jonathan




 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Wed Jul 27, 2016 7:59 pm