1. Home
  2. Computing & Technology
  3. Delphi Programming
Implementing Dialog Boxes in Asp.net web applications
Dialogs in an Asp.Net web application? MonoSoftware.Web.Dialogs is all you need! And it's free.
 Join the Discussion
"Post your views and comments to this chapter of the free Asp.Net Delphi Programming Course"
Discuss!
 Related Resources
• A Beginner's Guide to Asp.Net Programming for Delphi developers.TOC
 Elsewhere on the web
• Mono-Software - free asp.net web controls

Welcome to the sixth chapter of the FREE online programming course:
A Beginner's Guide to Asp.Net Programming for Delphi developers.
Producing a simple message box (like ShowMessage; or even an InputBox) in an asp.net application can be quite difficult - as you need to mess with DHTML, JavaScript and IE object model. It would be much better if we could write only one line of code (as in traditional desktop applications) to display a MessageBox ... let's see how.

ShowMessage, InputBox, MessageDlg ... ??!
Over the years of programming in the Win32 world of desktop applications you've probably get used to writing a line like "ShowMessage('Hello World');" whenever you need to display a simple message to the user. How about the MessageDlg function? We use those calls every day, and yet if you search for a similar functionality for your asp.net web application inside the vast collection of .Net (web) classes ... you'll find nothing alike!

Traditional desktop applications use these elements so often that their importance can be easily overlooked. From a developer's point of view, calling and retrieving return values from these objects is an easy job, usually requiring a single line of code. However, the emergence of Web-based technologies complicates this situation, and programmers are often forced to manually code client-side scripts to achieve similar functionality.

When building asp.net web forms, you are supposed to follow the server side model - where all event processing is done on the server. As explained before, this means that when a user enters something in the TextBox component placed on a web form, a postback (roundtrip to the server) must occur for you to be able to process the input.

Naturally, when building asp.net applications a developer should tend to write code that has the least possible number of postbacks involved in processing a user input (to lower the traffic, save the user time, etc).

A "problem" to consider
Picture the following situation: you have a text box (ok, Edit box) on a web form, a user enters some data in the text box, and selects the "Submit" button. As with any web button, a postback occurs, you process the data entered and decide to warn the user that the data he/she provided is not valid....
Ok, you could "unhide" a certain Label web control, set the text color to red and write "bad data". Wouldn't it be much more user-friendly if a message box might popup to tell the user the data is bad!
It would, of course. Unfortunately, the asp.net does not provide a ShowMessage functionality.
So, what to do?
One solution is to spend several hours of your time and code/create the needed functionality. Far better solution is to find the component (search the Net) that has all you need...

The solution to use!
Mono Dialogs from Mono-Software.com is one such solution. This ASP.NET component is designed to free you from the nightmare of ever-changing client-side scripts and bad-looking dialog boxes. And best of all, it is completely FREE!

"Warning"
Using third-party components in such an early chapter of the Course?! Client-side scripts? The discussion that follows in the article will touch some topics we haven't had time to explain yet - but don't runaway, as you will see the ideas and the implementation will look quite familiar.

MonoSoftware.Web.Dialogs
You can download this collection of common dialogs from Mono-Software for free. MonoSoftware.Web.Dialogs provide the most powerful way of adding professionally-looking ShowMessage, MessageDlg and InputBox controls to your ASP.NET applications.

After you download and install the component, locate the "Read me first.txt" file inside the installation folder. On a default install, that should be "C:\Program Files\Mono Ltd\MonoSoftware.Web.Dialogs". This file has some important steps you need to follow in order to be able to use Mono Dialogs in your asp.net applications.

We'll explore these steps in just a minute ... first, let's create a new asp.net web application.

MonoSoftware.Web.Dialogs - an example of use
If you have followed this Course from the beginning you have already created a sample asp.net application you can use to test the Dialogs functionality inside the MonoSoftware.Web.Dialogs package.

Load the "DelphiASPNetTest" project; or create a new asp.net application. Add a new Web Form to the project, name it "MonoDialogs.aspx".


Adding a new asp.net page to an existing application
To add a new web page to an existing Delphi for .net asp.net web application select the File - New - Other menu option from the main IDE menu. On the "New items" dialog window, select "Delphi ASP Files" (a sub item of the "Delphi ASP Projects") from the "Item Categories" tree view; then select the "ASP.NET Page" item and click OK.

New .. ASP.NET Page dialog in Delphi for .Net

Adding a reference to MonoSoftware.Web.Dialogs
Since this is a third-party assembly, to be able to use Mono Dialogs, you need to add a reference to the MonoSoftware.Web.Dialogs. Inside the "Project Manager" window locate and expand the "References" tree item. Right click it and select "Add reference".

Project Manager - References

The "Add Reference" window will be displayed, locate the "monoSoftware.Web.Dialogs" entry and click "Add reference" button, finally, select "OK".

Add reference dialog

Next, locate the added reference and mark the "Copy Local" option from the context menu.

Assembly - Copy Local

Finally, as described in the "Read me first.txt" file, you need to:

  • copy the "MonoDialogPage.aspx" file (located in "MonoResources/Dialogs" installation folder) to the root folder of your web application. Note that you do not need to explicitly include this file in the project.
  • copy the entire contents of the "/MonoResources" folder (along with this folder) to your Web site root folder. (in IIS: ...\inetpub\wwwroot).
  • copy the MonoWCLDialogs.css (located in "CSS Skins" installation folder) to the root folder of your web application.

    All set. Back to the project
    Now, add a ListBox and two Button components from the "Web Controls" group on the "Tool Palette" window. Name the two buttons "AddItemButton" and "RemoveItemButton" (read this as "change the ID property in the Misc group to..."). The ListBox could carry the default ListBox1 name.

    Mono Dialogs Test at design time

    The idea is to let the user enter new items in the ListBox, remove the selected item or delete all the items from the ListBox. We'll use the InputBox function from the Mono Dialogs package which displays a web based input dialog box that lets the user enter a string - in our case the item text. To enable item deletion from the list, we'll put MessageDlg in action. The MessageDlg function creates, displays, and operates a web based message box - the same way as you are used to in classic desktop applications.

    First, make sure you add "MonoSoftware.Web.Dialogs" to the uses list in the interface part of the web form page-behind class.

    Note: it would be wise to open the Documentation that comes with the MonoSoftware.Web.Dialogs component to get familiar with the dialogs and the call parameters.

    Next, add the following code to the Page_Load procedure:

    procedure TMonoDialogs.Page_Load
      (sender: System.Object; e: System.EventArgs);
    begin
      if not Page.IsPostBack then
      // only first time
      begin
        ListBox1.Items.Add('http://delphi.about.com');
        ListBox1.Items.Add('http://aspxDelphi.net');
        ListBox1.Items.Add('http://www.mono-software.com');
      end;
    
      NewItem := Dialogs.InputBox(AddItemButton,
                                  'Add an item to ListBox',
                                  'New item name',
                                  'A new item');
    
      DoRemoveItem := Dialogs.MessageDlg(RemoveItemButton,
          'Are you sure you want to remove the 
           selected item from the list?' + #13#10 +
          'Select [All] to remove all Items!',
          'Removing an item',
          TDlgType.mtConfirmation,
          TMsgDlgBtn.mbAll or 
          TMsgDlgBtn.mbYes or 
          TMsgDlgBtn.mbCancel,
          False);
    end;
    

    Let's see what's happening in the Page_Load event handler:
    First, we fill in some initial items in the List Box making sure that the code executes only *once* - or better to say only the first time we access the page. On every postback this code (the "if Not Page.IsPostback..." block) will not execute.

    Note: you need to add two unit level variables that hold the resulting values from the call to the InputBox and MessageDlg.

    ...
    implementation
    
    var
      NewItem: string;
      DoRemoveItem : TMsgDlgResult;
      
    ...
    

    Next, we attach an InputBox to a call to the AddItemButton. Every time a user presses a button - an InputBox will popup asking for a new item entry. If the user presses "Cancel" nothing will happen (postback "canceled"). However, if the "Ok" button is picked a postback will occur and the code in the AddItemButton's Click event handler will execute. The NewItem string variable holds the string value the user typed into the InputBox.

    InputBox in an asp.net application

    procedure TMonoDialogs.AddItemButton_Click
      (sender: System.Object; e: System.EventArgs);
    begin
      ListBox1.Items.Add(NewItem);
    end;
    

    To enable a user to remove the selected item from the ListBox we'll use the MessageDlg function. Again, the DoRemoveItem variable (of the Dialogs.TMsgDlgResult type) is used to hold the return value from a call to the MessageDlg function. Note that we indicated that three buttons should appear on the dialog. The Cancel button (by default) cancels the postback; the Yes button tells us that the user whishes to delete only the selected item; and the All button provides a user with an option to clear the list box.

    MessageDlg in an asp.net application

    If any of the All or Yes buttons we selected the code inside the RemoveItemButton's Click event handler will execute. This is what will happen ...

    procedure TMonoDialogs.RemoveItemButton_Click
      (sender: System.Object; e: System.EventArgs);
    begin
      if DoRemoveItem = TmsgDlgResult.mrAll then
      begin
        ListBox1.Items.Clear;
      end
      else
      begin
        if ListBox1.SelectedIndex > 0 then
          ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        else
          Dialogs.ShowMessage(Page,
                              'Nothing was selected',
                              'Operation failed',
                              TDlgType.mtWarning);
      end
    end;
    

    The code above uses the third dialog function from the Mono.Web.Dialogs assembly. The ShowMessage simply displays a warning to the user that nothing was selected.

    ShowMessage in an asp.net application

    And that does it! As simple as in any desktop application. Well, more or less... there are some rules you have to follow when building web applications. For example, you CANNOT attach an InputBox to a button in a Button Click handling procedure - simply because the dialog must be attached to the Button in an earlier event - like Page_Init or Page_Load. If you try to attach an InputBox to a Button inside its own Click handling procedure ... the InputBox will appear one postback too late.
    Hm, this sounds to much complicated for this chapter .. we'll deal with page processing and postback some other time.

    Hey, this is the .Net world!
    Final note: have I mentioned the language the Mono Dialogs are built with? I haven't! Why? It's simple: this is .Net - who cares abut the actual language (even though you have to admit that "TDlgType.mtWarning" looks quite familiar)!

    To the next chapter: A Beginner's Guide to Asp.Net Programming for Delphi developers
    That's it for today, in the next chapter we'll continue to explore the content of a web form: HTML controls, Web control ... and who know what else :) Stay tuned!

    If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

    A Beginner's Guide to Asp.Net Programming for Delphi developers: Next Chapter >>
    >> Web Forms - building blocks of an Asp.Net application (Part 2)

  • Explore Delphi Programming
    About.com Special Features

    Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

    Easy ways to connect two computers for networking purposes. More >

    1. Home
    2. Computing & Technology
    3. Delphi Programming

    ©2009 About.com, a part of The New York Times Company.

    All rights reserved.