tags:

views:

674

answers:

9

what in asp.net similar to message box

if (s != "null")
   MessageBox.Show("your in ");
else
   MessageBox.Show("wrong user ");

i want to replace message box here in c# code

+2  A: 

There is no message box as such in asp.net. The nearest thing to this kind of functionality will be a javascript alert box.

Andy Rose
A: 

There are no direct equivalent. The MessageBox class is a Windows Forms class.

In ASP .NET, a JavaScript alert would be most commonly used for the same purpose.

This runs on the client, so you would need to emit the javascript in the generated html, if you need to control the message serverside.

driis
+2  A: 

ASP.NET is a server side technology. The whole C#/VB/etc. code will be executed on the server and the response (which is probably HTML/JS/...) will be sent to the client.

You could send out the necessary Jscript code to display a message box on the client. Note that this code will not be executed immediately, but will be sent to the browser. The browser will interpret it and display the message box accordingly.

<script type='text/javascript'>
   alert('Hi');
</script>
Mehrdad Afshari
A: 

I think there are some nice examples how to accomplish this here:

Examples

Cj Anderson
A: 

For confirmations you could use the Confirm button from ASP.NET AJAX Control Toolkit

Kb
A: 

I found a fairly useful method of emitting javascript alerts from the code-behind in ASP.NET and modified it some. I don't know if it is an ideal solution or not, but you should just have to paste the function in your code-behind (the example is in VB).

Private Sub MessageBox(ByVal msg As String)
    Dim lbl As New Label()
    lbl.Text = "<script type="text/javascript">" & Environment.NewLine & _
               "window.alert('" + msg + "')</script>"
    Page.Controls.Add(lbl)
End Sub

To use, you would just call MessageBox("Here is your Input").

TheTXI
The language attribute is obsolete. Use type="text/javascript".
Guffa
Good catch. Seems like I copy/pasted this one from an older project I had been doing some maintenance on.
TheTXI
A: 

You would use the client script manager to run a Javascript when the page loads, which shows an alert box to the user:

Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "window.alert('You are in.');", true);
Guffa
A: 

ASP.NET Popup Message Box User Control - Really sexy looking ASP.NET server message box control. He also has a jQuery version.

Create Messagebox user control using ASP.NET and CSS - Nice guide on building your own.

Duke of Muppets
A: 

If you want the user to confirm an action you can use the onclientclick property of a button

<asp:button Id="btnConfirm" runat="server" Text="Confirm Me" OnClick="MyFunction" OnClientClick="return confirm('Are your sure?')" />
Adam Pope