Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Really simple question

Status
Not open for further replies.

VBGuy2112

Programmer
Feb 19, 2002
58
US
On a simple form, I have a button. I want a msgbox to appear and say "Hello" when the button is clicked. How can I do this in asp.net (in vb)?

Sorry for a simple moron question. I could have normal asp do it all-day-long but I'm brand new to .net.

Thanks.
 
I've been toying with this for part of the morning. I have a button that I want the user to click, and then display a confirmation messagebox. I ended up using a custom server control that I found at
It's written in c# but it is still easy to use if you program in vb. There is a nice article on how to use it at:

hth,
drew
 
Guys guys guys. When I get time I need to write an FAQ up for this.

It is sometimes a little difficult to do client side manipulation from a server side technology such as asp.net. Technically you can't!
However, just because you are using this cool new technology don't forget the old stuff. ie. Javascript.

All of the visual server controls in ASP.NET have an attributes property and that is what we are going to be using.

Remember, javascript has the ability to handle events too, this isn't a new feature to asp.net. In vbGuy's case we want to use the onclick event. The following code when placed in the server side form_load event using vb will place some client side javascript on the page that allows a button to display a message to the user ala message box.


If not me.IsPostBack then
btnGeorge.Attributes.Add("onclick", "javascript:popup('text goes here ')")
End If


This code will display a popup to the user. The reason it is put in the page load under the not Me.IsPostBack is so that to save resources it is only written out the first time the page is viewed. After that the control is simply redisplayed as it is.

If you require a custom message you can use string concatenation and variables to customize the message displayed.

Hope this helps you. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I appreciate your responses. I understand I need to use javascript, like I would have with normal asp.

I tried adding a normal button from the HTML toolbox and added the code you posted in the form_load event (see below). I still do not get a message box when I click on the button.

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs)Handles MyBase.Load
If Not Me.IsPostBack Then
btnMessageBox.Attributes.Add("onclick", "javascript:popup('text goes here ')")
End If
End Sub
 
oops sorry got my syntax mussed up. It should be alert not popup That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Oh, I feel stupid now. I knew it was alert instead of popup. Thanks for continuing to respond. I'm almost there.

I still don't get the message box when I click on the button on the screen. If I look at the code in the designer, the "javascript:alert('text goes here ')" appears as a clickable link and when I click on it, it shows the form and the message box appears. Why doesn't it appear when I right click the form and select "view form in browser" and click the button on the actual form? This is confusing.
 
Might be a caching problem. Try emptying your browsers cache and temp files. If that doesn't work view source while in the browser and check to make sure that the script is being written to the client.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I cleared the cache and temp files and I still don't get the message box. Any other things I can try?
 
Did you check the browser source to see if it's there?

Dim sb As New StringBuilder()

sb.Append(&quot;<Script language='javascript'>&quot;)
sb.Append(&quot;alert('btnConfirm')&quot;)
sb.Append(&quot;</Script>&quot;)
RegisterStartupScript(&quot;buttonClick&quot;, sb.ToString)

sb = Nothing


You could put this in the button.click event but it requires a post back.

Could I see code?
That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I looked at the browser source and I don't see that the onclick attribute was added.
I'll try the code with the postback.

Thanks!
 
there is already a faq on this:

faq855-2019

Good Luck Daren J. Lahey
Just another computer guy...
If you are unsure of forum etiquette check here FAQ796-2540
 
I have the same problem with the FAQ answer. When I'm looking at the code: &quot;javascript: alert('Are you sure you want to delete this record?');&quot;) ..... I can click on this code(like a hyperlink) and it brings up the form and shows the message box. When I bring the form up in the browser and click the button, it does not show a message box.

I'm almost there. Something just isn't right here. Any more suggestions? I appreciate everyones time and responses.
 
are you sure you are including the return key to the javascript?


&quot;javascript:return alert('Are you sure you want to delete this record?');&quot;


hth Daren J. Lahey
Just another computer guy...
If you are unsure of forum etiquette check here FAQ796-2540
 
Does anyone have a small example page they can post? Just a bland form with one button on it. When I click the button, a message box appears and says &quot;hello&quot;. I can't believe I'm having so much trouble with this simple concept.
 
Ah! I figured out the problem. The .net installation here at work is hosed (another co-worker confirmed that). I tied it here at home and I get the message box.

Thank you everyone for your replies and patience. Good, maybe I'm not as dumb as I thought. Nah, I probably am.

Thanks!
 
Just in case anyone has this problem in the future, the install did not map the aspx files to .net properly. None of the server code was being processed. That is why the msgbox was not appearing and I would get an error when I tried to debug saying that the server was not setup for debugging.

Cheers.
 
Glad you figured out. Have a good one! Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top