Dialog boxes with ASP

G

Guest

Hi,

I am trying to initiate a dialog box in an ASP web page. The code does some
processing and checks a few things, then if all is OK, asks the user to
confirm. For this I need it to pop up a simple OK/Cancel dialog box, read
the result, and act accordingly.

This would of course be very simple in a Windows application, but it seems
that is not the case in ASP. All the solutions I have found so far involve
running javascript when the inital button is pressed to pop up the dialog
box, and then passing the result to the ASP code for it to process.

Unfortunately I need to go to the ASP code first, do some processing, pop up
the dialog box if required, and return the result to the ASP code for it to
act upon. So basically I need to do something that will to all intents and
purposes act like a standard MessageBox.

I'm familiar with VB.NET and C#.NET, but this is my first time trying to do
an ASP.NET page.

Any help much appreciated!

Mat
 
K

Kevin Spencer

The JavaScript confirm() function will do what you want. It takes a string
(message) as an argument, presents a Message Box with an OK and a Cancel
button, and returns true if the OK button is pressed, false if the Cancel
button is pressed.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
G

Guest

I've come across that, but the trouble is I don't know how to call that
Javascript...

Say I have some simple javascript in the header of the ASP page, e.g.

function MyJavaScript()
{
if (confirm(msg))
{
return ("true")
}
else
{
return ("false")
}
}

and I reach the point in my ASP code where I need to get the user response,
how do I make my ASP code call that function and read the answer?

For example, in a windows app, I could use: -

DialogResult DR = MessageBox.Show("Hello", "", MessageBoxButtons.OKCancel);


I appreciate I may be missing something rather obvious, but thanks anyway!

Mat
 
M

Mark Rae [MVP]

and I reach the point in my ASP code where I need to get the user
response,
how do I make my ASP code call that function and read the answer?

You keep mentioning ASP - I presume you mean ASP.NET and not ASP...?
For example, in a windows app, I could use: -

DialogResult DR = MessageBox.Show("Hello", "",
MessageBoxButtons.OKCancel);

I appreciate I may be missing something rather obvious, but thanks anyway!

<asp:Button ID="cmdDelete" runat="server" Text="Delete"
OnClientClick="return confirm('Are you sure you want to delete this
record?');"
OnClick="cmdDelete_Click" />

When the above button is clicked, the user will be asked to confirm that
they want to delete the record. If no, nothing further will happen. If yes,
the page will postback and run the cmdDelete_Click method.
 
G

Guest

Thanks, but again I've come across this, and the problem is this time that I
don't want to ask the user to confirm the action when they press the button.

When they press the button, I want some ASP code to run that does several
things, and runs some checks, and only under certain circumstances pops back
with the confirmation request.

A greatly simplified but analagous example would be a form with a TextBox
and a "Submit" Button. When you click the button, some ASP code checks the
length of the text in the TextBox. If it is greater than 8 characters, it
"Submits" it (whatever that means in the context), but if it is 8 characters
or less, it pops up a box asking the user if they are sure, only "submitting"
it if they click "OK" or "Yes" or whatever.

I know the above example could be easily done with just JavaScript, but I am
just using it for an example.

And yes, I do mean ASP.NET. Sorry, I was being lazy!

Thanks again...
Mat
 
M

Mark Rae [MVP]

yes, I do mean ASP.NET. Sorry, I was being lazy!

It helps to avoid confusion if you say ASP.NET when you mean ASP.NET...

Anyway, for a JavaScript-only solution:
When they press the button, I want some ASP code to run that does several
things, and runs some checks, and only under certain circumstances pops
back
with the confirmation request.

<script type="text/javascript">
function validateForm()
{
if(document.getElementById('<%=MyTextBox.ClientID%>').value.length <
8)
{
return confirm('Are you sure you want to delete this record?);
}
}
</script>

<asp:Button ID="cmdDelete" runat="server" Text="Delete"
OnClientClick="return validateForm();" OnClick="cmdDelete_Click" />

If you need to check some stuff server-side (e.g. against a database),
you'll need to postback initially, do the server-side checking, and then
inject the JavaScript prompt via ClientScript.RegisterStartupScript:
http://msdn2.microsoft.com/en-us/li...riptmanager.registerstartupscript(vs.80).aspx
 

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

Similar Threads


Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top