Can anybody help me identify the code in ASP.Net that is equivalent to MessageBox (C#) or Alert (JSCript). I need a message box that will pop up when a certain condition is not fulfilled.

Your response is highly appreciated.

vb.net:

MessageBox.Show("Your Message Here")

C#:

StringBuilder str = new StringBuilder();
str.Append("<script language='javascript'>");
str.Append("alert('Your Message')");
str.Append("</script>");
RegisterStartupService("Msg",str.toString());

I tried the given code but I receive a message such as StringBuilder not found. Does it need an assembly or header file? What is it?

compiler cant found stringbuilder class since you didnt register the namespace. you can use the class' fully qualified name or you can use the using statement at the top of your code file like :

System.Text.StringBuilder s = new System.Text.StringBuilder();

or include the using statement above your class definition :

using System.Text;

then you wont get any compiler errors.

the easiest way to show a message box is this :

<asp:button id="foo" runat="server" text="foo" onclientclick="alert('message box is shown now')" />

i can help you with the details if you be more specific on what you want?

hi
as per my knowledge the best way for getting message box in asp.net project is creating a class and put there, following code.


'To display the message
Public Sub msgbox(ByVal msg As String, ByVal mypage As Page)
mypage.RegisterStartupScript("msg", "<script>alert('" & msg & "');</script>")
End Sub

then you can use this using object of this class any where in your project.

try this.

Best Luck. :)

Thanks. I experience some problems again. What if let's say a user had entered an invalid value in a textbox, then a MessageBox should pop up like "Sorry, minors not allowed!". How can I do this?

try this :
<script>
function doValidation()
{
var text = document.getElementById('<%= Textbox1.ClientID %>').value;
var retVal = false;
//forexamle let say you wont allow text longer than 10 characters
if(text.length > 10)
{
alert("the text entered is not valid");
retVal = false;
}
else
{
retVal = true;
}
return retVal;
}
</script>
<asp:button runat="server" id="button1" value="validate" onclientclick="return doValidation()" />

Thanks buddy! What if my script is C#? And I wish to create class for messagebox, I wish to repeatedly call it in any part of my program?

Thanks buddy! What if my script is C#? And I wish to create class for messagebox, I wish to repeatedly call it in any part of my program?

hi
i already provided you a code for creating a class for Message box in vb code.
you can get auto converted code from vb to c# using this link. try this...

http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx

Best Luck..

Thanks buddy! What if my script is C#? And I wish to create class for messagebox, I wish to repeatedly call it in any part of my program?

If you want to access this script from all the pages, put the script in a file with .js extention and then register the script to the master page of your application like
<script src="relativePathTo.jsFile"></script>
then you can call the same script from all your pages without repetition of the code.
It is not a good idea to embed javascript into c#

C#.Net or Vb.Net
Messagebox.show("hai welcome");

Equavalent asp.net coding

string srt="<script>";
str +="alert('hai welcome')";
str += "</script>";
response.redirect(str);


Regards,
Umapathy Perumal

If I make use of response.redirect will it not close my current page? Thanks...

if you are really determined to use messagebox as c# class method, create a class and put this code into it :

// for postback events
public static void ShowMessageBox(string _message)
    {
        Page page = HttpContext.Current.Handler as Page;
        page.RegisterStartupScript("key2", "<script>alert('" + _message +  "');</script>");
    }

// for async postback events
 public static void ShowMessageBoxForAsync(string _message)
    {
        Page page = HttpContext.Current.Handler as Page;
        ScriptManager.RegisterClientScriptBlock(page,page.GetType(), "key2", "alert('" + _message + "');", true);
    }

Then you can call this in, let's say , button_click event handler like this ( i assume that your class name is Utilities without any namespace definition) :
protected void myButton_Click(object sender, EventArgs e)
{
Utilities.ShowMessageBox("you entered a wrong password");
}

This question has already been answered. Start a new discussion instead.