What does this ClientScriptManager code do??

A

AAaron123

I read the help on which says:

The ClientScriptManager class is used to manage client-side scripts and add
them to Web applications...


But could use a little help. Can someone tell me what the code below is used
for?

If you could just put a few comments into the code that would help.

Is this code requires when ever the <script> tag is used?

The last line appears to relate to the following, but what does it do?

<asp:Image ID="MasterChurchImage" runat="server" AlternateText="Church
Image" ImageUrl="~/Images/MasterChurchImage.jpg" />

THANKS for any insight



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim csname2 As String = "ButtonClickScript"

Dim cstype As Type = Me.GetType()



Dim cs As ClientScriptManager = Page.ClientScript



' Check to see if the client script is already registered.

If (Not cs.IsClientScriptBlockRegistered(cstype, csname2)) Then

Dim cstext2 As New StringBuilder()

cstext2.Append("<script type=text/javascript> function DoClick() {")

cstext2.Append("Form1.Message.value='Text from client script.'} </")

cstext2.Append("script>")

cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), False)

End If

MasterChurchImage.Attributes.Add("onload", "resizeImg('" +
MasterChurchImage.ClientID + "')")

End Sub
 
C

Cowboy \(Gregory A. Beamer\)

Client script manager adds JavaScript to the page. It can be added in the
header or in the body, depending on which method you use.

The last lines here are adding an attibute to the MasterChurch image, which
is a way to add elements not recognized on the ASP.NET image tag which are
available in HTML. In particular, this addition adds an onload event handler
to the image tag. Examine the source when you run the page and you will see

<img onload="resizeImg('1')>

plus more (I purposely avoided writing the whole thing).
 
A

AAaron123

Thanks for the clear description. Now I read the Help again and see if it
now makes sense. I run this in
the Page_Load event.


I've tried and it appears that you can have multiple blocks like this each
with a different csname* and cstext*. Correct?



If (Not cs.IsClientScriptBlockRegistered(cstype, csname2)) Then

Dim cstext2 As New StringBuilder()

snip...

cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), False)

End If

MasterChurchImage.Attributes.Add("onload", "resizeImg('" +
MasterChurchImage.ClientID + "')")


Also, the ...Attributes.Add is outside the If..
If that is correct it appears register should be done only once but the Add
each time the page is loaded. Correct?


Thanks again
 
B

bruce barker

the if is just to test if the register call has already been made. you
are confusing two unrelated bits of javascript code.

the RegisterScriptBlock is used to render a javascript routine named
DoClick(). the second attaches an event handler (resizeImg) to an images
load event.

-- bruce (sqlwork.com)
 
A

AAaron123

I guess you're right I am confused.

For example, I have the following which works but I don't know why.

When I open the page the dialogbox does show.

I think I'm registering the script for PopupScript but I'd expect to have to
"call" it some place.

Registering doesn't cause it to execute does it?

Thanks for the interest



Partial Class Default_aspx

Inherits System.Web.UI.Page



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim csname1 As String = "PopupScript"

Dim cstype As Type = Me.GetType()

Dim csm As ClientScriptManager = Page.ClientScript

If (Not csm.IsStartupScriptRegistered(cstype, csname1)) Then

Dim cstext1 As New StringBuilder()

cstext1.Append("alert('This sites...es with text.');")

csm.RegisterStartupScript(cstype, csname1, cstext1.ToString, True)

End If

End Sub

End Class
 
A

AAaron123

In my other reply I should have mentioned that the below helped a lot.
You're right, I was trying to tie the two together and couldn't do it.

thanks again
 
C

Cowboy \(Gregory A. Beamer\)

You have two concepts here, as I see it:

Emitting blocks of JavaScript
Adding attributes to tags

Blocks of JavaScript can be created in a variety of ways. For example, you
can do something like this:

string code = "{block of code here}";
Literal lit = new Literal(code);
Page.Controls.Add(lit);

There is probably a mistake in that code, as I am just writing on the fly,
but it is one way to add JavaScript. The emit methods, like
RegisterClientScriptBlock are better, as you end up placing a script block
either a) at the top of the page or b) inline. There are two primary methods
here (on the ClientScriptManager class):

RegisterStartupScript
RegisterClientScriptBlock

But you can also avail youself of RegisterClientScriptInclude,
RegisterClientScriptResource, etc. All fo the methods are here:
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager_methods.aspx

That pretty covers outputting blocks, so to the attributes adding. This is a
way of adding tags not normally exposed by the ASP.NET classes. For example,
the code, you show sets up the following on the image:

onload="resizeImg('1')"
 
A

AAaron123

thanks a lot

Cowboy (Gregory A. Beamer) said:
You have two concepts here, as I see it:

Emitting blocks of JavaScript
Adding attributes to tags

Blocks of JavaScript can be created in a variety of ways. For example, you
can do something like this:

string code = "{block of code here}";
Literal lit = new Literal(code);
Page.Controls.Add(lit);

There is probably a mistake in that code, as I am just writing on the fly,
but it is one way to add JavaScript. The emit methods, like
RegisterClientScriptBlock are better, as you end up placing a script block
either a) at the top of the page or b) inline. There are two primary
methods here (on the ClientScriptManager class):

RegisterStartupScript
RegisterClientScriptBlock

But you can also avail youself of RegisterClientScriptInclude,
RegisterClientScriptResource, etc. All fo the methods are here:
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager_methods.aspx

That pretty covers outputting blocks, so to the attributes adding. This is
a way of adding tags not normally exposed by the ASP.NET classes. For
example, the code, you show sets up the following on the image:

onload="resizeImg('1')"
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top