×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Using confirm() with ASP.NET

Using confirm() with ASP.NET

Using confirm() with ASP.NET

(OP)
I had the following problem on an aspx page; I had to detect any changes made on a form but the form also included a dropdown box which was set for AutoPostBack in order to populated a Zip Code, City, etc.

The problem with the general checking of form elements and then confirming a change in records was that on post back the value returned became a default value on the form.

I had a hidden textbox hold the value 0 and 1, if a Zip Code was changed this textbox recieved a "1", otherwise it remained a "0".  Since it was the only textbox on the form that would ever hold the value "1" I could use this to test for Zip Code post back.

The solution was:

CODE

<script language=javascript>
  function checkF_status(myFrm) {
  var el, opt, i = 0, j;
  while (el = myFrm.elements[i++]) {
  switch (el.type) {
  case 'text' :
  case 'textarea' :
  if (el.value != el.defaultValue) return F_isChanged(myFrm);
    break;
   }
  }
  var j = 0;
  while (el = myFrm.elements[j++]) {
  switch (el.type) {
  case 'text' :
  case 'textarea' :
  if (el.value == 1) return(confirm("You have changed your records, click OK to save...or Cancel to..."));
    break;
   }
  }
 var ans = confirm("No changes have been made to your current reocrd. Click OK to continue...or Cancel to...");
 if (ans==true){
   return true;
  }
  else{
   return false;
  }
 }
  function F_isChanged(myFrm){
  return(confirm("You have changed your records, now they will be saved."));
  }
</script>

Just thought I'd share it as well in case someone down the road ran into this.  What I didn't like about it was the repetition of the "While" loop but I could not get a simple if...then to cooperate otherwise.

Note too that both confirms had to return true and false for form submittal.  BillyRay provided in one of his threads the elegant use of returning stictly the confirm value in place of a true and false statement (I thought that was pretty slick).

RE: Using confirm() with ASP.NET

(OP)
Left out one important step.  In order to process in code behind any "changes" the hidden textbox would have to take on the value of "1" for other changes other than the dropdown box assigning it after post back.  The final function should be:

CODE

function F_isChanged(myFrm){
 document.reqForm.Delta.value = 1;
 return(confirm("You have changed your records, now they will be saved."));
}

RE: Using confirm() with ASP.NET


Isadore,

Good on you for posting the solution - hopefully someone will find it useful.

If you're posting things like this that aren't questions, but helpful advice or tips, you should select the "Helpful Tip" option (the light bulb) when posting - otherwise people (like me) will mistakenly spend time reading a thread thinking the poster needed help.

Dan

Dan's Page @ Code Couch
http://www.codecouch.com/dan/


RE: Using confirm() with ASP.NET

(OP)
Dan - good point; sorry 'bout that.  One final note.

The hidden text didn't work, evidently because it was hidden; i.e.,

<input type="hidden" id="mytext" runat="server">

So when I made it visible, I couldn't get it to size (perhaps using CSS?), this didn't work:

<input type="text" size="0" id="mytext" runat="server">

At any rate, what I had to do was substitute the html text for an asp:Textbox, q.v.,

<asp:Textbox id="mytext" size="0" runat="server"/>

The latter was invisible on the web page and yet remained visible for the javascript to function.

RE: Using confirm() with ASP.NET

(OP)
One final issue is the differentiation of the a post back due to the dropdown and codebehind (AutoPostBack = submittal) relative to a submittal for saving a newly changed record.  My solution for this which capped the problem was to assigned an indendent number to indicate a non-dropbox submittal, q.v.,

CODE

......
var ans = confirm("No changes have been made to your current reocrd. Click OK to continue...or Cancel to...");
 if (ans==true){
   document.reqForm.Delta.value = 3;
   return true;
  }
  else{
   return false;
  }
 }

Hope this may help someone down the road; works like a charm here.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close