954,162 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can I use Msgbox function in ASP.NET ?

hi guys

I have heard that for displaying message we generally use MSGBOX function in VB.NET and
we use ScriptManager.RegisterStartscript in ASP.NET to display message box...
but I have tried to use Msgbox function in ASP.NET and it works here also...
So is it ok to use Msgbox function in websites to display message or
there are some problems or issues related to it?
If yes then pls let me know..
:)

mania_comp
Light Poster
44 posts since Jul 2009
Reputation Points: 8
Solved Threads: 1
 

No you can't. You need to use a client side alert() with javascript. You can read about client side events here: http://www.eggheadcafe.com/articles/20041016.asp

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Actually here is an example with an alert on a server side control. This will cause an alert then a postback:

Page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Alerter.aspx.cs" Inherits="daniweb.web.Alerter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    	<asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>


Code behind:

using System;

namespace daniweb.web
{
  public partial class Alerter : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Button1.Attributes.Add("onclick", "javascript:alert('hello');");  
    }
  }
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

ya i know that we should use alert function to display client side script in asp.net
but
i have msgbox function in my website and when I RUN the webpage it gives no error ..it works fine... ..
I want to know will that will it problems in real time deployment..??
I my PC that page runs well with MSGBOX function... but will it cause problems in realtime when website will be deployed to SERVER?

mania_comp
Light Poster
44 posts since Jul 2009
Reputation Points: 8
Solved Threads: 1
 

hey sknake,can u tell me one thing,that instead of writing Button1.Attributes.Add("onclick", "javascript:alert('hello');"); on button click event,Y we write it in Page Load event!

sandeep_1987
Newbie Poster
17 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
hey sknake,can u tell me one thing,that instead of writing Button1.Attributes.Add("onclick", "javascript:alert('hello');"); on button click event,Y we write it in Page Load event!


What?

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 
ya i know that we should use alert function to display client side script in asp.net but i have msgbox function in my website and when I RUN the webpage it gives no error ..it works fine... .. I want to know will that will it problems in real time deployment..?? I my PC that page runs well with MSGBOX function... but will it cause problems in realtime when website will be deployed to SERVER?


I don't know the answer to that -- go ahead and test it out. The MsgBox() call lies in the System.VisualBasic namespace and I do my best to avoid that namespace for compatibility reasons.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

i agree with snake...

but i'm curious why do u need a feature like MessageBox..?

or there is another way but it's a long way..

dnanetwork
Practically a Master Poster
Banned
633 posts since May 2008
Reputation Points: 28
Solved Threads: 106
 

'hey sknake,can u tell me one thing,that instead of writing Button1.Attributes.Add("onclick", "javascript:alert('hello');"); on button click event,Y we write it in Page Load event! '

This is used in the form load so that the Javascript is applied to the control once the form loads, otherwise this will only be applied to the control once you click on a button , therefore you will have to click the button twice.

1. to apply the JavaScript
2. to fire the javaScript

cVz
Junior Poster
140 posts since Mar 2008
Reputation Points: 29
Solved Threads: 7
 

The form_load event fires server side to create the page -- not when the form loads at the browser end. Its just adding a client side event to the button -- the asp:button's click event is normally server side.

Is this what you are asking?

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

hello dnanetwork , how r u ? Have u blocked,dat nobody can send u privat message?

sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 
Create a class and add these two methods to 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");
}
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You