Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 17th, 2006, 10:02 PM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default Client(javascript) from asp:button wont persist

Hello All: (My 1st post to forum 212 .)

For testing purposes, I want to change a document's background and foreground color by using a javascript function. Code shown below works fine. BUT: the color changes are only momentary.

A split second after the color change, the document colors revert to the colors before calling the javascript function. Thats the problem. How do I make the color change permanent?

Constraints: 1. Javascript must be in a separate .js file, not in a <script> block.
             2. Can't use an html button, have to use an asp:button.

Evjen's book: Professional ASP.NET 2.0 (Chapter 4) suggests using an html button. Book also seems to imply that one should use a client side callback. But there is no example for such callback using an asp:button.

Anyway, implementing the callback seems like an awful lot of overhead to go thru to do such a simple thing.

Complete project code is below (mercifully short). If anyone can suggest a simple way to achieve a permanent color change via the javascript function, called from an asp:button click without client call back, I'd sure like to hear about it.

Thanks.

Victor Victor
--------------------------------------------------------------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Default Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-size: 2em">JavaScript is Fun</div>
    <asp:Button ID="Button1" runat="server"
    Style="left: 68px; position: absolute; top: 80px"
    Text="Button"
    OnClientClick="set_colors()" />
    </form>
</body>
</html>
----------------------------------------------------------------------------------
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim meType As System.Type = Me.GetType()
        If (Not Me.ClientScript.IsClientScriptIncludeRegistered(me Type, "DefaultPageJavaScript")) Then
            Me.ClientScript.RegisterClientScriptInclude(meType , "DefaultPageJavaScript", "Default.js")
        End If
    End Sub
End Class
---------------------------------------------------------------------------------
function set_colors() /* This is in file: Default.js */
{
    window.document.bgColor = "red";
    window.document.fgColor = "yellow";
}
---------------------------------------------------------------------------------

  #2 (permalink)  
Old August 18th, 2006, 01:44 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi VictorVictor,

When you say "split second" second, what do you mean? Does it immediately change color again or after the next post back?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #3 (permalink)  
Old August 18th, 2006, 03:40 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Morning Imar:

Split second means less than 1 second.

In effect, the js function executes -- the screen turns red, the text yellow, but these changes are only visible for a fraction of a second. After the color change, the screen and text immediately change back to the colors in effect before the js function executed.
I believe a postback is happening and the viewstate colors which were not changed by the js function are used to re-render the page.

I have tested this little app by using an html button instead of the asp:button. With the html button the color change stays red and yellow.

I need to use an asp:button because in the real app on such a button click there will be much processing (like validating all the controls on a page) to occur -- client side. After all the client side processing occurs, then much server side processing needs to occur.

There are probably hoops I could jump thru, but nothing as simple as executing code client side with the OnClientClick attribute and server side with the OnClick event handler.

I have to go out of town today. I'll be glad to see your comments this evening on return. Thanks.

VV


  #4 (permalink)  
Old August 18th, 2006, 05:05 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I see. That seems to make sense. When you click the button, a client side function changes the background color. Shortly after that, the page is posted back to the server and then reloaded in the browser, effectively wiping out all client side customizations to the page.

It all depends on what you want to do. If you want the change to persist, change it in the button's server click. If you want to color to be persisted for a longer time, store it in the user's profile.

Alternatively, update the user's preference with Ajax, through the Atlas framework.

You have a lot of options, but I guess it all depends on what it is that you want exactly.

Cheers,

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: Disorder by Joy Division (Track 3 from the album: Heart And Soul (CD 1)) What's This?
  #5 (permalink)  
Old August 19th, 2006, 09:15 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar:

Yes. Now that I see what is going on I have something to work with
-- in this test case, a change to the color scheme on a page.
In the real app for which this small app is only a test platform, it is required that I use an asp:button. As I said earlier, there is a lot of processing that will occur in the server, once the client side js processing has finished for the asp:button click.

I guess what surprises me is that no one has mentioned this same problem. That is, the lack of programming ease when using an asp:button along with the idea to use clientside javascript processing for all the validation that can be done in the client. The saving achieved this way is to avoid as much processing as possible in the server, if that processing can be done client side.

So, I am going to see if the javascript function called via the OnClientClick attribute in the asp:button control can also change the values saved in the viewstate as well as the page itself. If I can: problem solved!!

Thanks for your help.

VV


  #6 (permalink)  
Old August 19th, 2006, 09:27 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I think you're mixing things up.

When doing validation, you should *always* validate on the server, whether you validate on the client or not.

Client side validation isn't validation; it's just a user's convenience for them to get a quick response about any mistakes they made. Only at the server can you truly validate the user's input.

Since it's too easy to bypass client side validation, you should never rely on it.

With regards to the buttons: a client side button runs at the client and a server side button runs at the server. If you have a button that posts back to the server, you shouldn't use it to make modifications to the DOM like change the page color. You should handle that in the server event.
Alternatively, if you want to change the color of something, use a true client side button, or stop the server-side button from posting back to the server (useful if, for example, you want to show an error message and stop the page from going to the server).

Does that explain things?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #7 (permalink)  
Old August 19th, 2006, 10:48 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar:
Oh my gosh! More food for thought, just when I thought I had it all nailed down.
You said: "...stop the server-side button from posting back to the server"
1. Could you give me a small idea on how to do that?

2. You're raising a major point that I now have to re-think. I don't see why validation cannot take place on the client side. I'm talking about verifying that a user has entered data into a field correctly. For example, a US zip code must be numeric and 5 digits. (There are exceptions to that, but lets keep it simple.)
Or verifying that no characters are present in a character field that would allow a <script> to be built and injected. These all seem to me to best be handled client side. Where is my thinking wrong on this?

In fact, the built in validation controls in VWD (Required field, Compare, Range...) to my mind can all be done on the client machine too, instead of sending the info off to the server.

3. You say that "it's too easy to bypass client side validation".
You're probably right, but I just don't see how anyone or anything can bypas the code I specify in a js function that is executing. I have a 3 inch high stack of help-file papers that I have lightly read (not scutinized), on the wide issue of security. My initial assessment was that I could do much processing client side and take it off the server. Now, your commments carry much weight with me. So, I am at a loss now on how to proceed with my idea to minimize server and maximize client processing.

4. The small app I posted to change the fore and background colors of a page was not truly representative of my larger project. But, that small app did represent my attempts to deeply understand the capabilities allowed by js client side processing. The color change is trivial, the ability to change (persist) DOM properties is not.

Finally, your last paragraph: "With regards to the buttons:...". I can't put both an html button and an asp:button on a page for a user to click, after he/she fills the page with data. One and only one button (click event) must do ALL the things that need doing for that page --both client and server side. Since the asp:button provides many serverside event handlers, that is the best one for most of the 70+ pages in my app.

Thanks again,(and you should not be working on Saturday).

VV



  #8 (permalink)  
Old August 19th, 2006, 11:11 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
quote:Thanks again,(and you should not be working on Saturday).
I know, I know. I am working on an article that I want to finish today... BTW, the same applies to you.... ;)

I'll address your questions / concerns below.

1. Consider this:
Code:
function CheckForm()
{
  if (form is valid bla bla)
  {
    return true;
  }
  else
  {
     alert('Ooops, not valid')
    return false;
  }
}

<input type="submit" onclick="return CheckForm();" value="Click Me" />
In this simple example, the form won't be posted when the it's not valid. (What "valid" means in this case is up to you). The trick here is the onclick handler for the button. When CheckForm returns true, you end up with:

onclick="return false;"

which stops the form from being posted.

2. The built-in validators do both. They run at the client, and, in server code you can do this to run them at the server as well:
Code:
Page.Validate()
If Page.IsValid Then

Else

End If
This way, you have the best of both worlds.

Your thinking is correct when you can guarantee that the validation script is executed at the client. However, this is not always the case.

3. There are many ways to stop client scripts from running. You can change your browser settings, for one. Or you can install the IE Dev Toolbar which has a convenient "disable script" option. With the script disabled, most pages (with input type="submit" buttons) still submit to the server.

Finally, I can create an HTML document on *my* machine with this code:
Code:
<form method="post" action="http://www.victorvictor.com/SomePage.aspx">

  <input type="text" name="txtZipCode"
     value="I can send whatever junk I want here, including <h1> 
        HTML</h1> and even <script>alert('Hello world');</script>. None 
       of this is ever validated    " />
</form>
If you accept the Zip code without server side checking, this junk can end up in the database. If you also display it somewhere publicly, the next visitor is in for a surprise.

I get around 20 e-mails a day with an error message from my site that is triggered by this type of code. Spammers try to inject links to their dubious sites on my Contact page and Talk Back pages. Spammers don't enable JavaScript because your site asks them to; they just send whatever they see fit to your site and see where it lands.

See where I am getting with this? Scared already? You should be... ;)

Therefore, you should treat client side validation as a convenience for the user only. Use server side validation to truly validate your data from a business perspective.

4. Correct, this may not be trivial. Again, it all depends on what you're trying to do, and what client changes you need to persist.
Quote:
quote:One and only one button (click event) must do ALL the things that need doing for that page --both client and server side
Why is that? Can't you have multiple buttons that carry out different or overlapping tasks?

Hope this sheds some light.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #9 (permalink)  
Old August 19th, 2006, 11:25 AM
Friend of Wrox
 
Join Date: Apr 2005
Location: Fort Walton Beach, FL, USA.
Posts: 190
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK. I'm going to print your latest and take a look at it later, word by word. I've got to understand everything about this before I can safely proceed.

(Worries me much that putting a website up with interactive capablity allows for someone somewhere to do nasty things; but that's the real world.)

Thanks, and good luck with the article.

VV


  #10 (permalink)  
Old August 19th, 2006, 11:33 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Good plan. Although it's only a few paragraphs of text, it may be hard to digest and understand what this means for you and the applications you design.

Feel free to post follow up questions...

Indeed it's like real life. Client side validation only is like taping a letter with the text "Forbidden for thieves" on your unlocked front door. Somehow, thieves never seem to listen to these kind of messages... ;)

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: Thieves Like Us by New Order (Track 6 from the album: Substance - Disc 1) What's This?
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a button in javascript in asp.net ? coolcoder2007 ASP.NET 2.0 Basics 0 October 26th, 2007 04:14 AM
In ASP,Client side javaScript code casimir Classic ASP XML 0 October 24th, 2005 12:24 AM
How to add client-side script to an ASP button BrianWren VB.NET 1 March 30th, 2005 05:27 PM
sessions wont persist bogins Beginning PHP 2 April 22nd, 2004 01:56 PM
ASP Upload - does object persist across pages? James Diamond Classic ASP Components 4 January 27th, 2004 09:41 AM



All times are GMT -4. The time now is 01:26 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.