views:

348

answers:

5

I've just started using ASP, so while programming the code behind in C# I was surprised to find out that MessageBox was not available to me to make a confirmation popup triggered by a button click. While googling I noticed a lot of suggestions to use Javascript, but was hoping to avoid adding another language to my code. I tried to implement the suggested Javascript, but must be missing something in the syntax.

I did find one that was all C#: C# MessageBox It compiles, but does not pop up an alert. I'm still trying to see if I can make it work. I was calling it as a button event and thought perhaps the event wasn't being hit, but I checked it in page load also and still nothing popped up. Any suggestions?

A: 

ASP is a server side language. Anything you want to do on the client side has to be either html/css, javascript (or any other client side, broswer supported, language).

Esteban Araya
+1  A: 

You need to learn about what happens where. The C# code is run on the server, and the result is sent to the browser. Select view source in your browser to see what is being sent from the server.

To show a popup you need to send a small snippet of javascript to the browser that will show the popup. It might however be a better idea to show a box at the top of the page with the message instead, as it's not as intrusive.

svinto
A: 

Attach the alert to the OnClientClick event of the button. So myButton.OnClientClick = "alert('message')"; If you want to prompt the user, like, "are you sure?" use a confirm instead of the alert:

myButton.OnClientClick = "return confirm('are you sure?')";

David
A: 

You can use onclick="retrun window.confirm('Your message here');"

Neil
My button is in a gridview. Is there an equivalent for onclick for a gridview button?
VengefulSakhmet
+1  A: 

You want to use javascript to do your client side validation. I used to use the ASP.net controls to do the same but they are less flexible and bulky.

Declare a block in your code to place your javascript function.

This is your

<script language="javascript"></script>

block.

Your button control will also need an onClientClick attribute that points to that javascript function while the onClick attribute will still point to your C# function in the code behind.

The button's xhtml should look like this.

 <asp:Button ID="Button1" runat="server" OnClientClick="confirm_entry()" onclick="Button1_Click" Text="Button" />

Hope this is what you're looking for.

gsirianni
Can I have two scripts on one page? I already have one for my c# code. I'm using a gridview buttonfield. :-PThank you for your help.
VengefulSakhmet
Yes you can. You can have your C# code and your javascript code. I tried to send you the example page but the UI on stack overflow won't display it properly.
gsirianni
Your code behind will contain the event hanlder for the button's onClick attribute. The onClientClick attribute will handle the javascript portion. The <script language="javascript"></scipt> block will be located inside your XHTML portion of your code. Place it after the <head></head> tag.
gsirianni