Click here to Skip to main content
15,886,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to display a msgbox in asp.net using C#?
I can't do it using windows.msgbox
It can be done by,
Response.Write("<script>alert('Item Already Exists')</script>");

But the problem I am facing is that, when the msgbox is being displayed, the actual webpage turns blank (i.e. full white), and on clicking OK, I am getting back the page again. Is there any way to display the msgbox as well as the page at the same time?
Posted
Updated 7-Sep-12 4:06am
v2
Comments
ZurdoDev 7-Sep-12 10:09am    
This is because you are invoking it from C#. What you could do is use jQuery and use the document.ready() function so that it fires when the page has finished loading.

Well try with:

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);


Also check this article:

Simple MessageBox functionality in ASP.NET[^]

Cheers
 
Share this answer
 
v2
Comments
Member 9413361 7-Sep-12 10:54am    
msgbox("your message ") it also work
Mario Majčica 7-Sep-12 11:12am    
are you sure about that? In an ASP.NET project?!?!?
Aritra Nath 7-Sep-12 16:33pm    
i dont think msgbox works in asp.net
that was mentioned in my question
Aritra Nath 7-Sep-12 16:36pm    
@mario.. ur code works fine, i.e. i am getting the msgbox and the main page at the same time, but somehow while the msgbox is displayed, my masterpage menu alignment is getting mixed up. its coming back to normal after i click ok. any idea what is happning?
You cant do Response.Write since it wipes out the content and put the response as what you are sending inside the Write method.

Instead lets say you have a button in your asp.net page and on click of the button you want to show the message box from C# then,You can do this on the button click event in C#

C#
string javaScript = "alert('Item Already Exists');";


          Page.ClientScript.RegisterStartupScript(this.GetType(),"ButtonClickEventScript", javaScript,true);
 
Share this answer
 
Comments
Aritra Nath 7-Sep-12 16:38pm    
ur code works fine, i.e. i am getting the msgbox and the main page at the same time, but somehow while the msgbox is displayed, my masterpage menu alignment is getting mixed up. its coming back to normal after i click ok. any idea what is happning?
Ashraff Ali Wahab 7-Sep-12 17:27pm    
This is because the script which aligns the menu gets executed after our alert script gets executed. That is always the problem with RegisterStartupScript since it doesn't guarantee order.
Aritra Nath 8-Sep-12 1:02am    
so any idea on how to fix this?? or is there any other way to display the msgbox so that the menu alignment stays intact??
Ashraff Ali Wahab 8-Sep-12 16:55pm    
Call the javascript function which aligns the menu before calling this alert statement.
hello, use the following code.

XML
protected void Page_Load(object sender, EventArgs e)
{
  string message = "Hello! Vinod.";
  
  ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", message);
}



Thanks!!!!
 
Share this answer
 
You have to add reference of using " system.windows.forms " to use messagebox.show feature.
It very simple.
for more questions mail me at " saurabhdhir3@gmail.com "
 
Share this answer
 
v2
Comments
[no name] 6-Apr-13 7:55am    
The messagebox would be displayed on the server computer not on the client side for the user to see. What would be the point of that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900