Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9
This is the forum to discuss the Wrox book Professional ASP.NET 2.0 Special Edition by Bill Evjen, Scott Hanselman, Devin Rader, Farhan Muhammad, Srinivasa Sivakumar; ISBN: 9780470041789
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 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 March 15th, 2006, 12:07 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP.NET Server Controls and Client-Side Scripts

Hi,

I would apprecieate some help on the following problem.
I am building a web application for database access. I would like to be able to prompt the user to save changes, etc...
I am using Javascript to do make a confirm dialog and store the result of the user's selection in an html input field called txtMessageBoxResult.

<script type="text/javascript" language="javascript">
    function confirmSubmit() {
        var doc = document.forms[0];
        var msg = "Are you sure you want to submit this data?";
        if (confirm(msg))
        {
            doc.txtMessageBoxResult.value = "Confirmed";
        }
        else
        {
             doc.txtMessageBoxResult.value = "Denied";
        }
    }
</script>

I have bound the Javascript function to btnNext in the PageLoad event:

btnNext.Attributes.Add("onClick", "return confirmSubmit();")

This works great except for one thing. Right after the user clicks OK or Cancel, txtMessageBoxResult receives the correct value (either 'Confirmed' or 'Denied').
HOWEVER, since the Button_Click event causes the Page_Load event to occur again, the value stored in txtMessageBoxResult is lost.
I tried to store the value in an ASP.NET control, as in listing 4-9. I deleted the html input field, added an ASP.NET label to the page and named it txtMessageBoxResult. But now when the user clicks ok or cancel, the label is not updated.

I know there must be a simple solution to this problem, but I can't find it. I've Googled around and tested different solutions for about 4 hours now, and I can't justify spending that much time to get a message box to work!

BTW, I am extemely frustrated at the way a Button_click causes the page to load again. If anyone knows a good reason for this, please let me know. If I at least knew there was a reaon for it, it might make it a little easier to deal with, since there is apparently no way around it.

Thanks,
Mike
  #2 (permalink)  
Old March 15th, 2006, 02:47 PM
Wrox Author
 
Join Date: Dec 2005
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The ASP.NET Button causes a postback because it is really rendered in HTML as <input type="submit">, which causes form post.

If you don't this to happen, and don't need any other server side functionality, then I would just use a regular HTML button (<button>), rather then the ASP.NET Button control.

Devin
  #3 (permalink)  
Old March 15th, 2006, 03:09 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Mike,

Every post back control (e.g. LinkButton, Button, selected index changed of a drop down list with AutoPostBack on etc etc) *always* cause Page_Load to fire. That's how the whole .NET architecture works.

Inside Page_Load, you can determine whether the Page_Load is triggered with by a normal, initial Load, or by a PostBack, using Page.IsPostBack.

If all you want to do is stop the page from submitting, your confirmSubmit function should return either true or false. With a false value, the form won't be submitted.

If you do want the page to submit, but need to use the value (Confirmed or Denied), then use an asp:Hidden field and give that a value in your JavaScript function. Then on the server you can access that value again and see what action the user chose.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #4 (permalink)  
Old March 15th, 2006, 03:44 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Devin, thanks for the speedy reply. It makes more sense now. I started programming ASP.NET w/o ever learning html, so I am a bit behind the curve on html basics.

Mike
  #5 (permalink)  
Old March 15th, 2006, 05:35 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thanks, Imar, for the reply. I have found another way to solve my problem, but I am still interested in making this solution work. I understand what you are saying:

<<If you do want the page to submit, but need to use the value (Confirmed or Denied), then use an asp:Hidden field and give that a value in your JavaScript function. Then on the server you can access that value again and see what action the user chose.>>

However, when I use the follwing code with a asp:HiddenField I could not make it work. The script never seemed to update the hidden field. It was always blank.

<script type="text/javascript" language="javascript">
    function confirmSubmit() {
        var doc = document.forms[0];
        var msg = "Are you sure you want to submit this data?";
        if (confirm(msg))
        {
            doc.HiddenField1.value = "Confirmed";
        }
        else
        {
             doc.HiddenField1.value = "Denied";
        }
    }
</script>

Any ideas? I also tried
 doc['HiddenField1'].value="Confirmed";
 document.forms[0]['HiddenField1'].value="Confirmed";

One other question. Say I did'nt want to call the script from a button.click event, but elsewhere in the code, and receive the return value. Is there any way to do this?

I appreciate the replies.
Thanks,
Mike
  #6 (permalink)  
Old March 15th, 2006, 05:51 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Mike,

You need to take a look at the HTML for the hidden field. It's quite possible that .NET renamed it to something other than HiddenField1. This depends on where the control is located (e.g. in repeating data controls, a master page and so on).

Can you post the final HTML for the hidden field from the browser?

You can add onclick handler to many other controls and elements in your pages as well; you're not limited to a button....

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 


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
Is client-side validation in ASP.NET 2.0 possible? jacob ASP.NET 2.0 Basics 6 April 10th, 2007 05:07 AM
VBScript for Client-side controls in ASP.net cJeffreywang ASP.NET 1.0 and 1.1 Professional 1 March 22nd, 2007 08:02 PM
Print file at client side in asp.net manish.sharma04 ASP.NET 2.0 Basics 0 April 4th, 2006 08:46 AM
CLIENT SIDE TIME CALCULATION IN ASP.NET gmk51080 Javascript How-To 1 December 1st, 2004 01:29 AM
ASP.NET server side controls me_shriram VS.NET 2002/2003 3 August 11th, 2003 03:55 AM



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


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