Adding MessageBox to web application

  • Thread starter Thread starter Don
  • Start date Start date
D

Don

How can I add a Messagebox to my web application?
I have a datatable in my web application and I'd like to show a messagebox
when the table's empty.

Thanks.
 
Don,

In this case, you would have to inject script into the response stream
to show a messagebox. You want to call the alert method on the window
object that is exposed to the browser script through JavaScript.

You register your script in ASP.NET by calling the RegisterStartupScript
method on the Page class.

Hope this helps.
 
Don said:
How can I add a Messagebox to my web application?
I have a datatable in my web application and I'd like to show a messagebox
when the table's empty.

In javascript use window.alert(sMessage).
 
Thanks for the info.

I'm not very familiar with Javascript. For example, in my CSharp code I ask:
if (myTable.Rows.Count <= 0)
{
//Display Window.Alert("empty table");
}

I'm not sure where to put this Window.Alert();
Is it in the .aspx code or the csharp code?
 
I think I have it, but it doesn't display the alert:

if (myTable.Rows.Count <= 0)
{
sMessage = "Employee doesn't exist";
Response.Write("<script language='javascript'> {
window.alert(sText) }</script>");
return;
}
 
Don said:
Thanks for the info.

I'm not very familiar with Javascript. For example, in my CSharp code I ask:
if (myTable.Rows.Count <= 0)
{
//Display Window.Alert("empty table");
}

I'm not sure where to put this Window.Alert();
Is it in the .aspx code or the csharp code?

If you want to do it in C Sharp, then do as Nicholas suggests and use
RegisterStartupScript block. If you want to do this without using C
Sharp then you would need to put the some logic in the javascript in
your aspx file to determine when to show the message.
 
Thanks for the example but I'm really not sure of what you're trying to tell
me. Let's say I have a button in my web application and I want to display a
MessageBox, what would I need to add in my C Sharp code (not the aspx file)
?

private void Button2_Click(object sender, System.EventArgs e)
{
//code to display a Messagebox here that says "Hello worlld"
}
 
Don said:
Thanks for the example but I'm really not sure of what you're trying to tell
me. Let's say I have a button in my web application and I want to display a
MessageBox, what would I need to add in my C Sharp code (not the aspx file)
?

private void Button2_Click(object sender, System.EventArgs e)
{
//code to display a Messagebox here that says "Hello worlld"
}

What you need to understand is that there is no way to do this without
client side script. The decision to make is where do you put that
script. You can either put it directly in the aspx file or you can
write some C# code that will inject the script into the response stream
that gets sent to the browser. You must understand that fundamental
concept in order to continue.

There are helper functions in the .NET framework that can aid you in
injecting javascript into the response stream.

If you wanted to put it in your server side click handler it might look
as follows:

private void webButton_Click(object sender, System.EventArgs e)
{
RegisterStartupScript("webButtonClick", "<script
language=\"javascript\">window.alert('you clicked the button');</script>");
}

If the action isn't going to change based on dynamic content that can
only be determined server side after the button is clicked, then it
would be more efficient to either modify the aspx file directly, or add
an attribute to the button in your Page_Load event. Ex:

private void Page_Load(object sender, System.EventArgs e)
{
webButton.Attributes.Add("onclick", "javascript:window.alert('you
clicked the button');");
}
 
I am sure you mean that you want to display a message in a box, which
you could do so with the help of the alert function of Javascript

alert("Some Message");

MessageBox is an object of the windows application domain and hence,
cannot be used in the web model.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top