DropDownList Confirmation

A

Andy

I have been struggling with this for a while, and have seen lots of
related postings, but nothing (as far as I can see) that answers this
directly.

Im using vb dotnet and have an aspx that has a drop down list. For the
selected item in the listbox a number of textboxes are populated from
the database. The user can change the data in one or more of these
text boxes, after which they should press the button that saves he
data if they want to commit those changes.

What I want to achieve is that when the user selects a different item
in the drop down, the syetm should prompt with a confirmation to ask
whether the user wishes to save the changes. If the user selects yes,
then the save is run (in the code behind) before moving to the newly
selected item in the drop down. If the user selects no then the newly
selected item is displayed with no save.

I have the code behind that does the save, populates the screen etc. I
can get a javascript confirm working, but I cant bring it all together
to achieve exactly what I am describing above.

Any help would be very gratefully received.
 
V

Vidar Petursson

Hi

<select onchange="if(confirm('Save?')) this.form.SUBMITBTNNAME.click()"
If yes it will click the submit button

Or perhaps

<select onchange="this.form.bDoSave.value = confirm('Save?');
this.form.SUBMITBTNNAME.click()"
<input type="hidden" name="bDoSave"....
Then you can check the hidden element on the server....

--
Best Regards
Vidar Petursson
==============================
Microsoft Visual: Scripting MVP 2000-2004
http://www.icysoft.com/
http://www.deus-x.com/ Instant e-commerce
http://www.microsoft.com/technet/scriptcenter/
Playground: http://213.190.104.211/ ( IE 5.5+ only )

No matter where you go there you are
==============================
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

If you want to have the confirmation popup only when the user change at
least one of the controls then you need to do some javascript scripting,
create a variable at page level:
var anychange = false;

then you have to add an onchange handler for ALL the controsl you want, you
can do this in the code behind using:
textbox1.Attributes.Add( "onchange" , "setchanged();")

finally in the dropdownlist you should do the same:
textbox1.Attributes.Add( "onchange" , "CheckIfChanged();")

in this function you show the confirmation if the anychange variable is
true, and submit the form if needed.

Cheers,
 
A

Andy

So, the onchange event for the textbox sets the flag anychange to
true. The dropdownlist, when changed, fires the event which checks the
flag.

When testing, I took the approach that I would ask the user if they
wanted to save and triggered the following on listbox change.

<script>
function AskUser()
{
var answer = window.confirm("Do you want to save?")
document.forms(0).answer.value = String(answer)
document.forms(0).submit()
}
</script>

But I had 2 problems

1) I couldnt work out how to get the True\False from the confirm to
either save then go and get the new data for the list box item, or
just go and get the new data for the list box item (without saving).

2) At the point at which the above event fires, the list box has
already changed, and Ive lost the original listbox item, which is what
I need when running the save in the code behind.


I hope these questions are sensible and that somebody can put me out
of my misery.

:-(
 
V

Vidar Petursson

Hi

Ok.. why not put the ID of item edited into a hidden input
as the dropdown will change ..

<html>
<head>
<script>
function doConfirm(f){
var bIsDirty = 0;
var e;
// You can check element type & compare defaultValue to value
// The defaultValue is what you populate on the server
// for simplicity I only check type = text/textarea
// radio/checkbox have defaultChecked which you can check also
for(i=0;i<f.elements.length;i++)
{
e = f.elements;
if(e.type == "text" || e.type == "textarea") if(e.defaultValue !=
e.value) bIsDirty = 1;
}
if( bIsDirty ) f.bDoSave.value = confirm("Item changed!!\n\nDo you want to
save?") ? 1 : 0;
f.submit();
// If you are using validators you can call the click method of the
submitbtn
// instead of f.submit();
// do f.mySubmitBtn.click();
}
</script>
</head>
<body>
<form>
<select onchange="doConfirm(this.form)" name="mySelect">
<option value="1">blah1</option>
<option value="2" selected>blah2</option>
</select>
<input type="hidden" name="myHiddenID" value="2"> <!-- Store ID here, same
as the selected one in the dropdown -->
<input type="hidden" name="bDoSave" value="0"> <!-- push save flag here,
default 0 -->
<input type="text" name="myInput1" value="SOMEVALUE">
<input type="text" name="myInput2" value="SOMEVALUE">
<textarea name="myTextArea">blah blah</textarea>
</form>
</body>
</html>

More info:
http://msdn.microsoft.com/library/d...hor/dhtml/reference/dhtml_reference_entry.asp

--
Best Regards
Vidar Petursson
==============================
Microsoft Visual: Scripting MVP 2000-2004
http://www.icysoft.com/
http://www.deus-x.com/ Instant e-commerce
http://www.microsoft.com/technet/scriptcenter/
Playground: http://213.190.104.211/ ( IE 5.5+ only )

No matter where you go there you are
==============================
 
A

Andy

OK

I understand the Isdirty concept !
I understand the ability to store the values in hidden fields

But how is the save in the code-behind running. Is this your
f.mySubmitBtn.click\f.submit ?

I have the save in the code_behind, and I want that to be executed
using the value in the hidden field as the key, and the other text
boxes as the data fields.

Sorry if Im being a bit thick here - but if you can explain this last
bit to me Ill be there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top