Monday, March 26, 2012

Why not use MsgBox?

Hello, I'm new to ASP.NET and I have a doubt that I would like you to help me sort out.

I have noticed that ASP.NET programmers never resort to message boxes to inform or alert their users of what is going on. They make use of label controls instead.

Now, I have tried to use the VB6 statement MsgBox and it works perfectly. For example:


MsgBox("This is my message.", vbInformation, "Message from the programmer")

Since I suppose there is a reason why ASP.NET programmers prefer to avoid using that kind of message box, I'd like to ask you if the keyword MsgBox is not compatible with operating systems different from Windows or if there is any other reason which prevents most of you from using it.

TIAYes there is a very good reason why no-one uses it. It doesn't work. If you put in a VB6 style message box the code executes server side, meaning that the message box opens on the web server and not the client. The reason why it appears to have worked for you is that you probably are testing your application (client-web server) on the same machine.

If you want a message box to pop up I suggest you use the javascript alert() method.

hope this helps,

sivilian
Thanks very much for this important lead.

Could you tell me how to create the alert JavaScript function and how to call it in ASP.NET (VB) code?

TIA
That's easy to do. Simply add an attribute to the control which you want to execute the pop-up. Here is an example:


myButton.Attributes.Add("OnClick", "javascript:alert('Pop Up Text');");

hope this helps,

sivilian
I'm afraid I'm new to this kind of programming, so I really need a step-by-step example.

Suppose I have a button called btnHello and I want to get a message box when I click on it, what steps should I take? Please don't take anything for granted.

TIA
In the page load of your application put in the above code, but replace 'myButton' with btnHello. This will insert the javascript code that will display a message box when the button is clicked. If you have trouble let me know the specifics and I will try to help you more.

sivilian
I did as you said and it works fine.

Thanks a lot.
myButton.Attributes.Add("OnClick", "javascript:alert('Pop Up Text');")

Without ; at the end of the line - than it work's

regards from Berlin
Stephan
Dear sivilian, I need to display a message box not as soon as a button is clicked but after it executes some code, e.g. after it changes the Text property of a label control.

I have tried to use your code to achieve such a result, but unsuccessfully.

I have put the line of code you provided me with in the Click event of the button. Unfortunately, it was only executed after I clicked twice on the button.

The above mentioned code runs as follows:


Sub btnMsgBox_Click(sender As Object, e As EventArgs)
lblMsg.Text = "New text for the label."
btnMsgBox.Attributes.Add("OnClick", "vbscript:MsgBox('Hello World!')")
End Sub

Is there any way to modify the code above to make it work?

TIA

P.S. As you can see, I have changed javascript to vbscript, but I think the two script languages are equivalent, aren't they?
well, try to put the btnMsgBox.Attributes..... on page load...or try this:


imports system.text

Sub btnMsgBox_Click(sender As Object, e As EventArgs)
lblMsg.Text = "New text for the label."
dim strBuilder as new stringbuilder

strBuilder.Append("<script language='javascript'>")
strBuilder.Append("{alert('Hello World!');}")
strBuilder.Append("</script>")

RegisterClientScriptBlock("PopUp", strBuilder.ToString)
End Sub


Thanks, your code looks great.

The only problem I have encountered is, when I click on the button I get the message box but immediately afterwards the button disappears!

How can you amend it?

TIA
I still have to sort out how to create a working message box in ASP.NET.

Any help will be appreciated.

Please consider I am using Web Matrix which (I suppose) may have some limitations in comparison with the VS.NET environment.

TIA
Which of the button disappears?
Check if you did not assign a value to it in the Page_Load override. This would reset all settings you make elsewhere !

0 comments:

Post a Comment