Click here to Skip to main content
5,786,882 members and growing! (25,102 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » Controls     Intermediate

Dynamic AJAX Modal Popup

By Kelum W. Ganegoda

Dynamic AJAX Modal Popup control demonstration
Javascript, XML, HTML, C# 2.0, C#, Windows, .NET, .NET 2.0, Visual Studio, ASP.NET, WebForms, Ajax, Dev

Posted: 22 Aug 2007
Updated: 22 Aug 2007
Views: 62,926
Bookmarked: 50 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 1.84 Rating: 1.84 out of 5
4 votes, 40.0%
1
2 votes, 20.0%
2
2 votes, 20.0%
3
1 vote, 10.0%
4
1 vote, 10.0%
5
Screenshot - AajxModalpopup.jpg

Introduction

Asynchronous JavaScript and XML (AJAX) -- Jesse James Garrett was the creator of the term AJAX -- enables a rich user interface experience for web applications. I'm not going to tell you the plus and minus points about AJAX, but this will help you to inform yourself about AJAX's background.

Here I demonstrate the usage of the Modal Popup control, one of the AJAX controls that comes with AJAX Toolkit 1.0. The reason that I was motivated to write this article about the Modal Popup control is that I came across a situation where, by selecting a dynamically populated list, it was desired to display more details about the selection as a modal dialog box. The challenge is that I was developing a web application and I had only two days to achieve this. Thanks to AJAX Modal Popup, I made it. Otherwise, I couldn't make it within two days because there would be too much researching and tons of JavaScripts. I think this article will help those who are in the same situation.

Background

The scenario is a real world scenario: when a user selects a court from the drop down list, he needs to view more information about that court without redirecting to a new page. Also, until he closes the dialog, he should not be able to edit background items on the form.

Using the Code

Before trying this demo, please do these prerequisites:

  1. Add the MS SQL 2005 database by selecting Add New Item…
  2. Using the Server Explorer, add a new tale called Court
  3. Add the following table definition
    Column Name            Data Type
    -----------            ---------
    CourtID (primary key)  int
    CourtName              varchar(100)
    Judge                  varchar(50)
    Info                   varchar(255)
  4. Find the stored procedure SP.txt in the demo project and execute it. This is optional because you can use any method to get data populated.
In order to fetch data to the popup control, you have to create a web service. In that web service, add the System.Web.Script.Services.ScriptService attribute to the web service class. It should look like this:
[System.Web.Script.Services.ScriptService]
public class CourtService : System.Web.Services.WebService 
{
}

Create a web method that fetches the data. It should return a string and have a string parameter; this is mandatory. The string parameter is the dynamic key that is used to fetch data on demand. In our case, it's the dropdown's selected item.

[WebMethod]
public string GetCourt(string contextKey)
{
    //TODO. Method body

}      

If this method returns a string, how are we going to get all the data that we want to display? It's a simple, but tricky. On the popup control we are using a web server control, usually the Label control, to display data. Since this method returns a string, format your data to a line of string. As in the demo project I want to bold the column name, so I can wrap column the name with <b></b> HTML tags. Use the System.Text.StringBuilder class to format your string.

The completed method follows.

[WebMethod]
public string GetCourt(string contextKey)
{
    if (contextKey == "")
        return "";

    m_courtData = m_court.GetACourtByID(int.Parse(contextKey));
    
    DataSet1.CourtRow _courtRow = m_courtData.Rows[0] as DataSet1.CourtRow;
    System.Text.StringBuilder _sb = new System.Text.StringBuilder();

    _sb.Append("<b>Court Code: </b>").Append(_courtRow.CourtID);
    _sb.Append("<br/>");
    _sb.Append("<b>Judge Name: </b>").Append(_courtRow.Judge);
    _sb.Append("<br/>");
    _sb.Append("<b>Court More Info:</b><br/>");
    _sb.Append(_courtRow.Info);

    return _sb.ToString();
}

Pay attention to the following markup code in the Default.aspx page. Table 1 describes these properties respectively.

<cc1:ModalPopupExtender ID="mpeCourt" runat="server" 
    OkControlID="btnDlgOK"
    PopupControlID="pnlPopup" 
    TargetControlID="btnDummy" 
    DynamicServicePath="CourtService.asmx" 
    DynamicServiceMethod="GetCourt" 
    DynamicControlID="lblInfo"
    BackgroundCssClass="modal" 
    DropShadow="true">
</cc1:ModalPopupExtender>

Table 1: Property Descriptions

Property Description
OkControlID This refers to a button used to dismiss the Modal Popup. By setting this property, the dialog box will automatically close. This property doesn't show in the Properties window in the IDE. Use markup view to add this property. (Optional property)
PopupControlID This refers to the web server control ID that works as the Modal Popup, usually a panel control. This property doesn't show in the Properties window in the IDE. (Mandatory property)
TargetControlID This refers to the control that makes the dialog popup. Once we click on the target, the control pops up the modal dialog. As you can see, I set this to a button control. This is another trick. We can put a target control to our dropdown but, if we do so, once you click on the dropdown list it will popup the dialog. When that happens, you can't select an item. Since this is a mandatory property, I can't ignore this. To achieve our goal, I set this property to a button and hide it when the page is loaded, as follows.
btnDummy.Style.Add("display", "none");
DynamicServicePath This refers to the web service path. This property doesn't show in the Properties window in the IDE.
DynamicServiceMethod As its name implies, this refers to the web method of the service. This property doesn't show in the Properties window in the IDE.
DynamicControlID This refers to the control that displays strings returning from our web method. This property doesn't show in the Properties window in the IDE.
BackgroundCssClass This does the actual thing to make the rest of the page non-editable. This property doesn't show in the Properties window in the IDE. It accepts a CSS class name, like below.

.modal
{
    background-color: Gray;
    filter:alpha(opacity=40);
    opacity:0.7;
}
DropShadow This is set to true if you want to show the shadow of the dialog. This property doesn't show in the Properties window in the IDE. (Optional property)

As you can see, we use an instance of button for TargetControlID of ModalPopupExtender. Since this button doesn't display on the page, you have to code Modal Popup to show it. For our scenario, we want to show the dialog when a user actually selects an item from the dropdown. The following code demonstrates this.

protected void drpCourt_SelectedIndexChanged(object sender, EventArgs e)
{
    if (drpCourt.SelectedIndex != 0)
    {
        mpeCourt.DynamicContextKey = drpCourt.SelectedValue;
        // Display the dialog

        mpeCourt.Show();
    }
}   

Notice that DynamicCotextKey sets to SelectedValue of the dropdown, which is passing to the web method's parameter contextKey. Since the dropdown causes a post-back, the panel control PopupControlID will display on the page where that resides before Modal Popup gets centered to the browser. To avoid this issue, set this in the Page_Load event, as shown.

pnlPopup.Style.Add("display", "none");       

History

  • 22 August, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kelum W. Ganegoda


I am MCSD for .NET since 2004. I was freelance developer since 2002 to 2005. Projects was done by Ms-VB 6.0 and C#.Net.

I'm join to the Sri Lanka's largest apperal exporter. My key duies are there, intergation of ERP(Movex), Biztalk and latest .Net technologies such as Silverlight, WCF etc.
Occupation: Software Developer
Company: Brandix Lanka Pvt Ltd.
Location: Sri Lanka Sri Lanka

Other popular Ajax and Atlas articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
QuestionAJAX Modal Popup OR DHTML Windows.memberBobb198219:27 3 Sep '08  
AnswerRe: AJAX Modal Popup OR DHTML Windows.memberKelum W. Ganegoda20:01 9 Oct '08  
Generalhimembermanoj221840:22 22 May '08  
QuestioncontextKey passed null in web methodmemberMember 27752042:47 2 Apr '08  
AnswerHow to Fix the Example CodememberMember 396673811:38 20 Feb '08  
GeneralExample is brokenmemberBarbeque Source8:07 25 Oct '07  
GeneralIssuesmemberJonDays4:12 28 Sep '07  
GeneralRe: IssuesmemberKelum W. Ganegoda4:30 28 Sep '07  
JokeGood Idea ..memberSuresh Kojhani22:17 5 Sep '07  
QuestionKeep getting 'Web Service call failed: 12030'membermajhoos4:00 5 Sep '07  
AnswerRe: Keep getting 'Web Service call failed: 12030'memberKelum W. Ganegoda5:09 5 Sep '07  
GeneralRe: Keep getting 'Web Service call failed: 12030', or 500, or 12031memberJon Ebersole6:03 8 Nov '07  
GeneralRe: Keep getting 'Web Service call failed: 12030'memberthuongmaiinternet0:01 12 May '08  
Questionpostbackmembertoretang0:06 4 Sep '07  
AnswerRe: postbackmemberKelum W. Ganegoda18:56 4 Sep '07  
GeneralGood idea but..memberparadoxxl15:08 31 Aug '07  
GeneralRe: Good idea but..memberKelum W. Ganegoda19:03 2 Sep '07  
GeneralRe: Good idea but..memberTimMoore14:25 28 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Aug 2007
Editor: Deeksha Shenoy
Copyright 2007 by Kelum W. Ganegoda
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project