Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 21st, 2006, 12:22 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default How 2 Add Dynamic JScript to a WebForm

I would like to add something like the following to a page to have the ability to post an alert on the user's screen.
Code:
<script>

    var msg = ""; // <——<<< I want to be able to modify what's between 
                  //        the quotes when sending the page.

    function PostMessage()
    {
        if (msg != "")
        {
            alert(msg);
            msg = "";
        }
    }

</script>
</head>

<body onload="return PostMessage();">

    . . .

Not sure how to modify the contents of msg at the server...
Seems to me there's a problem also with including script like that. (Maybe what I'm recalling is the difficulty adding things like onkeypress to asp: controls. Whatever I'm remembering involved adding properties to the controls at runtime...)
 
Old April 23rd, 2006, 01:24 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Look into Page.RegisterStartupScript and Page.RegisterClientScriptBlock methods in the VS Help.

 
Old June 5th, 2006, 02:33 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I don't want to register a block, I just want to fill in a small group of characters at that specific location in the rendered stuff going to the client. Is there some way of doing that (ala WebClass replaceable tags)?
 
Old June 5th, 2006, 03:57 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Brian,

You can use old skool ASP delimiters and call a method in the Code Behind like this:

var msg = '<%= YourServerSideMethod() %>';

where YourServerSideMethod is a method that returns a string.

Alternatively, you could add the text to, say, a server side div and then use document.GetElementById to get the text and alert it.

That said, the RegisterClientScriptBlock suggested by jbenson001 makes a lot of sense. The entire PostMessage could be registered in the block, including the variable text. The benefit of that is that you only need to send the block when there is a message. That way, you don't need to check for "if (msg != "")" and better yet, you eliminate the need to send the block at all when there's no message.

HtH,

Imar
 
Old June 6th, 2006, 02:53 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

That’s pretty good reasoning...

(I just hate to do it that way though, because then the main functionality is not there, lying on the surface. Rather you have to go track down what is going to happen when the machine encounters that instruction to add the script block.)

Thanks.
 
Old June 6th, 2006, 03:30 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, I agree. It makes it harder indeed to see what's going on where.

That's what I like so much about .NET: there are so many ways to do the same thing, there's always a way to do it that suits you best... ;)

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Beating of my heart / Moa X by Various Artists (Track 13 from the album: Ibiza Lounge) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me...I have an error in webform subhasps8 ASP.NET 2.0 Professional 1 July 4th, 2007 02:24 AM
Add dynamic css to master/content page proj-How? VictorVictor ASP.NET 2.0 Basics 6 June 9th, 2006 12:09 PM
Add Dynamic Control HELP HELP jimmyfrank Access VBA 1 January 26th, 2005 06:05 PM
Show WebForm johanyu ASP.NET 1.0 and 1.1 Basics 5 October 14th, 2004 01:07 AM
refresh webform Abhinav_jain_mca General .NET 1 August 3rd, 2004 05:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.