Click here to Skip to main content
16,021,211 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi..
I'm converting my project into 3 layer architecture ,bu I'm stuck here that how can i show message dialog box in asp.net c# Class .cs file.I have a dataservice calss library,How can i show message box in class

What I have tried:

I try this in aspx.cs file its working well,here is javascript code
C#
Response.write(<"Script>alert('Welcome')</Script>");

but when i try in class .cs file but its not working.I want to show message box dialog here: dataservice.cs file
C#
if (resultObj != null)
{
   int.TryParse(resultObj.ToString(), out databaseID);
   //SHOW MESSAGEBOX HERE 


}
Posted
Updated 7-May-16 1:17am

If you're looking to get some kind of confirmation, or an alert before you continue your code then you're going to have to split your process in two, the first half will result in an html page being shown to the user that will show the alert and have a method to continue with the rest of the process. Some examples of doing this are below

Asking the user to confirm that they want to continue with an action | The ASP.NET Forums[^]
 
Share this answer
 
Maybe this help you

C#
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
 
Share this answer
 
Comments
Richard Deeming 7-May-16 7:56am    
You need to make sure the message is properly encoded to avoid XSS:
sb.Append(HttpUtility.JavaScriptStringEncode(message));

Also, you shouldn't have spaces within an HTML attribute:
sb.Append("<script type='text/javascript'>");
Refer this similar question which i have answered earlier today.

Show error/exception message in ASP.NET webform project.-CodeProject[^]
let me know if it helps.
 
Share this answer
 
JavaScript alert is a bad idea. It's fine, at best, as a temporary tool used during development process, but hardly flexible enough and good looking enough for the code of production quality. This is good if you want something rudimentary.

Perhaps the best idea would be some facility collectively known as "modal pop-up"; one of the most popular implementation is jQuery Dialog, but there are much more other implementations; many of them are 3rd-party jQuery UI plug-ins, featuring many effects, like transitional, and, notably, dimming. There are similar "models" in other JavaScript libraries. Basically, such products emulate modal behavior, in certain specific sense of this word, on the same Web page, which is a big benefit: there are no pop-up windows, nothing in the browser blocks such behavior.

Please read my article on the topic: Modal Popup From Scratch.

Not only I offer my own fully-functional product, but I also explain the conceptions, how it works, and explain the rationale behind all kinds of such "pop-ups", so you can decide what to use by yourself, or develop your own.

See also: Modal popup.

—SA
 
Share this answer
 
v2
Comments
Member 12369816 6-May-16 10:58am    
Ans my question?How to show message box pop up in class .cs file???
Dave Kreskowiak 6-May-16 11:12am    
You can't.

Anything that shows up in the browser window has to be done by client-side code. You can't show anything from your server-side code.

ASP.NET code runs entirely on the server. All it does it generate HTML/script pages and sends that to the browser for it to display. It's a rather simple process called "request-response". The browser requests a page from a URL, the server-side code listening at that URL generates the HTML and send it back to the browser. The browser then renders the HTML on screen. There is no continual connection between the browser and the server and no method that a web server can push content to a client without a request.
Sergey Alexandrovich Kryukov 6-May-16 12:32pm    
Thank you for this helpful comment.
I tried to provide more detail explanation of the issue in my new comment below, but obviously the inquirer needs to invest decent time to learn how things work.
—SA
Member 12369816 6-May-16 11:00am    
and you provide me dialog jquery which works in html but i want to show message box in code behind
F-ES Sitecore 6-May-16 11:09am    
You can't show an alert in your code-behind, it would help to learn and understand the asp.net page lifecycle. Your code behind generates html in one go, ie your code-behind runs until the whole html is created, that html is sent to the browser to show and it is only then that the js can run and show your alert.

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