Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2004, 03:27 AM
Authorized User
 
Join Date: Jan 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to creative_eye
Default How to display a Message Box

I'm working on ASP.NET using VB.NET in Visual studio environment. I want to display a message box. I have wrote the following code on a button's click event:

msg("some message here")

But this doesn't display the message. Instead, it displays an error. Can anybody tell me about the solution.

 
Old February 25th, 2004, 04:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You'll have to think a bit about the ASP.NET architecture before you realize the cause of your problem. ASP.NET runs on the server, while you view the output / pages in your browser at the client. If you were able to pop up a dialog from ASP.NET code, it would pop up at the server. And guess who has to close those zillion dialogs again... ;)

So, to have an alert pop up in the client, you'll have to send some JavaScript. You can add something to the attributes collection of a button. For example, you can add code that will be attached to the onclick attribute for an input element. Let's look at an example. Suppose you have a simple button on an ASPX page:
Code:
<asp:Button id="Button1" runat="server" Text="Click Me"></asp:Button>
When clicked, the following code will run at the server:
Code:
private void Button1_Click(object sender, System.EventArgs e)
{
  // Do server side stuff here.        
}
As I explained before, you can't pop up an alert here, because this code runs at the server. To fix that, you need to add the following code to the Page_Load event:
Code:
Button1.Attributes.Add("onclick", 
   "return(confirm('Are you sure you want to submit?'));");
When the page loads, the button gets an extra attribute called onclick in the client side code. When you run the page and click the button, you'll see a dialog box asking for confirmation. If you click OK, the page will submit, otherwise it stays where it is. If you look at the source in the browser (in the client), this is what you'll see:
Code:
<input type="submit" name="Button1" 
  value="Click Me" id="Button1" 
  onclick="return(confirm('Are you sure you want to submit?'));" />
  The code for the onclick has been attached to te button at the browser, providing for all this magic.
In this example I used the confirm method to ask for confirmation, but you can also use the JavaScript alert method if you want just a dialog with an OK button.

Does this help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 26th, 2004, 03:10 AM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<input type="submit" name="Button1"
  value="Click Me" id="Button1"
  onclick="return(confirm('Are you sure you want to submit?'));" />
 
Old February 26th, 2004, 04:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi aliarifpk,

I may be missing something here, but isn't your answer the end result of my initial post?
If you add that code directly to the page, it's not a Server control, so it won't fire stuff at the server, right?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old May 7th, 2004, 10:58 PM
Registered User
 
Join Date: May 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi...I do have the same problem, I want to display a confirm box but not onClick event.I am comparing one textbox value with another value and if the value does not match then only I shud call the javascript function to display a confirm box asking the user to change the textbox value.msg: "Do you want to change the text box value?" If the return value of confirm box is true, then I shud allow the user to change the text box value else I shud store all the values on the form and submit. Please help me..thanx!

 
Old February 8th, 2005, 12:37 PM
Registered User
 
Join Date: Feb 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank's Imar... it worked fine...

 
Old February 10th, 2005, 07:42 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have the same problem as Wish. I'm doing a comparison of the user's input to database values on pageback. If the dataset is empty, I want to display an alert box to tell them that login failed. Right now, I'm using a lame workaround, sending users to a login failed page. This is not optimal for visually impaired users.

Is there a way to produce a message box on postback?

 
Old February 12th, 2005, 01:50 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Sanchaz,

Take a look at this FAQ: http://imar.spaanjaars.com/QuickDocId.aspx?QUICKDOC=355

I think the second item, Displaying a Message Box at Load Time, is what you're after.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: In November by Front 242 (Track 13 from the album: Geography 2) What's This?
 
Old February 17th, 2005, 07:23 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I had the same issue. There's a good article by Tim Stall (aspnet.4guysfromrolla.com/articles/021104-1.2.aspx) that includes code for both confirm and alert msgboxes.

I needed to display an alert box after doing a database comparison, too. I used the code in on postback page load. My code is below.

BTW: some development environments (like DreamWeaver) choke on reserved words in strings. That's why I had to split the word 'script' into two pieces. This was not original thought. I found it in a response to someone else's question re: msgboxes.

<code>
Sub Page_Load(Src As Object, E As EventArgs)
dim MyName as string
If IsPostback then
If (dsauth_user.RecordCount <> 0) then
'CREATE A COOKIE - build later

'Create a session variable
MyName = dsauth_user.FieldValue("firstName", Nothing)
Session("userFirstName") = MyName
Response.redirect("Student_Page.aspx")
Else
Dim strMessage as String
strMessage = "The system did not recognize your user name and/or password."
Dim strScript As String
strScript = "<scr" & "ipt language=""JavaScript"" type=""text/javascript"">" & vbCrlf
strScript += "alert(""" & strMessage & """);" & vbCrlf
strScript += "</scr" & "ipt>"

If (Not Page.IsStartupScriptRegistered("clientScript")) then
Page.RegisterStartupScript("clientScript", strScript)
End if

End If
End If
End Sub
</code>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Count in combo box(display results in text box) mboyisis Access 4 April 4th, 2008 07:08 AM
resubmitting how to display message box tsuersrb Excel VBA 1 March 10th, 2008 07:06 AM
How to display message box in query? tsuersrb Excel VBA 0 February 7th, 2008 02:17 AM
Display Japanese characters in a message box kalyanipradeep Excel VBA 0 September 22nd, 2006 04:14 AM
Message display rodmcleay Forum and Wrox.com Feedback 1 June 5th, 2003 06:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.