Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using confirm() with ASP.NET

Status
Not open for further replies.

Isadore

Technical User
Joined
Feb 3, 2002
Messages
2,167
Location
US
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).
 
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."));
}
 
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


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top