Thursday, September 4, 2008

Adding a dailog box to a webcontrol

To create a dialog box in a page, we need to use javascript to run on the browser and to add a javascript to a web control, we use webcontrol.attributes.add as in btnDelete.attributes.add("onclick", "return confirm('Are you sure you want to delete?');")

It is important when placing the javascript to end it with a ";". This is because, if the webcontrol autopostback attribute is set to true, the .Net will automatically add "__doPostBack(..." javascript to the control. The ; will be a valid divider to allow both javascripts to coexist and your added javascript will run first.

As for the rest of the java script, you can either add into your page via the literal webcontrol or code it directly into your aspx file.

One example to remind the person to add skirting in an automobile purchasing website could be as follows:

rbCarOpt.Attributes.Add("onclick", "alert('Did you remember to add skirting?');")

When the person selects Car (as opposed to truck), it will pop up a dialog asking if the customer remember to add the skirting option.


If you want to have the alert appear in two lines then add \n to add new line. As in:

rbCarOpt.Attributes.Add("onclick", "alert('Did you remember to add skirting?\nThis will make your car look nicer.');")

will appear like this:

No comments: