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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MsgBox in ASP.NET 5

Status
Not open for further replies.

leassaf

Instructor
May 20, 2001
49
IL
How do I display a message box (Yes/No) on a web form, so that I can get a decision based on the user’s choice?

Assaf
 
You need to do this with Javascript. If you want to dynamically create the javascript you can do that with the RegisterStartupScript command. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Well it depends on when you want to pop up the message box. If you want it on a button click then you add an attribute value for the onclick event. Returning false if no to say don't submit the form so the serverside event won't happen.

Or you can add the javascript to the page then store the result in a hidden textbox and get that value server side when you need it.

If you can give a bit of background info I can give you a more specific example. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hi Mark!
The purpose of the web form is to add a Category record to the “Categories” table in the NorthWind database. Once all the fields information was filled and the user clicks Submit (Add customer), a message box should appear asking “Add product”. If the user clicks “yes”, then the appropriate panel control for “Categories” is displayed + a submit button to add the product. When the user clicks that button, the product is inserted to the “Products” table and the user is asked again (with a message box) whether to add another product etc. Once the user clicks “no”, the last product is added to the database.

I’ve used:
MessageBox.Show ("Add Contacts?", "New customer", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)

This demanded to include the using statement for the “System.Windows.Forms.dll”.

But whenever clients press the “Add customer” button, the message box appears on the IIS computer and not on their machine. I’d rather find a solution using MessageBox but right now I can’t see any clue.

Any ideas?

Assaf
 
There is no solution using messagebox. Remember that you are coding a serverside application not a desktop one. Any code that you write is run on the server. Some of that code in the case of a webform sends a stream of data down to the client where the browser looks at the data stream and renders that data stream as the browser sees fit.

So when you want something to happen on the client you add a control on the serverside that appends data onto that data stream that is being sent down. Or you can manually add data to that stream. One of the methods you can do that is to use the RegisterStartupScript command.

What this does is to append the exact string you send down right before the closing tag for the form. In most cases I have seen so far this is used to send down a piece of javascript. The browser then see's that javascript and does something with it.

Sorry for being so simplistic here but it really helps if you can understand this concept.

Now in your case we aren't going to use RegisterStartupScript. We are still going to append some data to that stream going to the client, just using a different method.

We are going to use the attributes property that every server control has. The following code should be placed in the page_load event under a not ispostback check.

Button1.Attributes.Add("onclick", "return (confirm('do you want to?'))")

Now to explain. Attributes is a collection so we access the add method of that collection to add an attribute. Simple enough. The parameters for an add include the Key and the Value. The key you want to specify is onclick for the javascript onclick event. The value is what we want to happen on click. Basically a confirm message box. By putting return in front we decide what will happen next. Should the client click Cancel in the messagebox the clientside onclick returns a false and so does not submit the form. If the client clicks OK then onclick returns true and the form is submitted.

Then server side in the event handler for the button you can add the Category and display the Product panel.

Do the same thing for the product ok button as we did here and you'll be fine.

I definetely hoped this helped ya my fingers are tired. j/k

Good luck That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thank you so much, Mark, for the detailed guidance. It threw lots of light on the whole concept of Client/Server sides.

I got it nearly done...

Assaf
 
glad to help That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Mark:

Button1.Attributes.Add("onclick", "return (confirm('do you want to?'))")

is very helpful. I was able to use it in a situation where you decide to Submit the form or NOT all. Like say ..

"Do you want to delete?" ...
IF Yes ... Submit
else ... DO NOT submit ...(i.e. Cancel )

However there is another case where you ALWAYS want to SUBMIT but do different processing based on the answer. For example..

"Add Sales tax? "

If Yes
CalculateRebate(Price + Tax)
else
CalculateRebate(Price)
end if

In such a scenario how can you use the Messagebox type solution. Is this where RegisterStartupScript can be used?. Or can you still use Confirm?

Please help with example!

Mikey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top