Working with ModalPopup Ajax Control
page 2 of 5
by Electronic Screw
Feedback
Average Rating: 
Views (Total / Last 10 Days): 26946/ 1011

Displaying the Master information

Create a new ASP.NET AJAX-Enabled website. Using a TableAdapter (available in the source with this article), ObjectDataSource and a GridView we display all the countries available in the database. The ASP.NET source for this is given below.

Listing 1: ASP.NET code for displaying the Country information in a GridView

<%--Countries Display--%>
<asp:Label ID="lblCountries" runat="server" Text="Countries"
    SkinID="Heading"></asp:Label><br />
<asp:UpdatePanel ID="upCountries" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" 
            AutoGenerateColumns="False" DataKeyNames="Id"
            DataSourceID="ObjectDataSource1" 
            OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" 
                   SortExpression="Name">   
                    <ItemStyle HorizontalAlign="Left" Width="80%" />
                    <HeaderStyle HorizontalAlign="Left" Width="80%" />
                </asp:BoundField>
                <asp:TemplateField>
                    <ItemStyle HorizontalAlign="Center" Width="20%" />
                    <HeaderStyle HorizontalAlign="Center" Width="20%" />                    
                    <ItemTemplate>
                        <asp:ImageButton ID="showCities" CommandName="Select" 
                           runat="server" ImageUrl="~/images/add_details.gif" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>                
    </ContentTemplate>
</asp:UpdatePanel>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetCountries" 
    TypeName="ModalPopupTableAdapters.CountriesTableAdapter">
</asp:ObjectDataSource>

In the GridView1 we have a templatefield with an ImageButton (ID "showCities"). To use this button to show the cities for the corresponding country row, we set the CommandName="Select" and use the OnSelectedIndexChanging event of the GridView1.

First we will look at the .aspx source that we need to add to display the Cities in ModalPopup.

Listing 2: ASP.NET code to display cities

<%--Cities Popup Display--%>
<asp:Button id="btnShowPopup" runat="server" style="display:none" />
<cc1:ModalPopupExtender ID="mdlPopup" runat="server" 
    TargetControlID="btnShowPopup" PopupControlID="pnlCities"
    CancelControlID="btnClose" 
    BackgroundCssClass="modalBackground"></cc1:ModalPopupExtender>
<asp:Panel ID="pnlCities" runat="server" style="display:none;" 
  SkinID="PopUpPanel">
    <asp:UpdatePanel ID="upCities" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="lblCities" runat="server" Text="Cities" 
                SkinID="Heading"></asp:Label><br />
            <asp:GridView ID="GridView2" runat="server" 
              AutoGenerateColumns="False" DataKeyNames="Id" Width="50%">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" 
                        SortExpression="Name">
                        <ItemStyle HorizontalAlign="Left" />
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                </Columns>
            </asp:GridView>
            <asp:ObjectDataSource ID="odsCities" runat="server" 
                OldValuesParameterFormatString="original_{0}"
                SelectMethod="GetCitiesByCountryId" 
                TypeName="ModalPopupTableAdapters.CitiesTableAdapter">
                <SelectParameters>
                    <asp:Parameter Name="countryId" Type="Int32" />
                </SelectParameters>
            </asp:ObjectDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>            
    <div style="text-align: right; width: 100%; margin-top: 5px;">
        <asp:Button ID="btnClose" runat="server" Text="Close" Width="50px" />
    </div>
</asp:Panel>

In the above code, the first line of markup is a normal ASP.NET button control with display style set to "none." I will tell you why I hide this button after explaining the next mark up.

Listing 3

<cc1:ModalPopupExtender ID="mdlPopup" runat="server" 
    TargetControlID="btnShowPopup" PopupControlID="pnlCities" 
    CancelControlID="btnClose" 
    BackgroundCssClass="modalBackground"></cc1:ModalPopupExtender>

Note: Make sure you have added a reference to AjaxControlToolkit.dll in your project and registered it in you page with directive

Listing 4

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" 
TagPrefix="cc1" %>

In the ModalPopup extender control, the TargetControlID is the control which activates the ModalPopup. Since we want to activate the ModalPopup from the ImageButton button in the Countries GridView and cannot access the ClientID of the control at design time, I created a dummy button and set its display style to none. The code for showing the modal popup is in OnSelectedIndexChanging event of the GridView1 which will be explained later.

The PopupControlID is the control that we want to display as Popup. In the above markup, it is set to "pnlCities" which contains the GridView2 to display the Cities.   

The CancelControlID is the id of the control that will cancel the modal popup. Inside the panel we have an asp:Button with ID "btnClose" that will cancel the modal popup. (Try to find why I kept the "btnClose" outside the UpdatePanel "upCities").

BackgroundCssClass is the css style that will be applied to the background when the Modal Popup is displayed.

We have all the ASP.NET code, so now we need to write the OnSelectedIndexChanging event of the Countries GridView to show the Cities in ModalPopup.

Listing 5: Code for GridView SelectedIndexChanged

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
  int _countryId = Convert.ToInt32(GridView1.DataKeys[e.NewSelectedIndex].Value);
  odsCities.SelectParameters["countryId"].DefaultValue = _countryId.ToString();
  GridView2.DataSource = odsCities;
  GridView2.DataBind();
  upCities.Update();
  mdlPopup.Show();
}

In the above code, find the selected countryId through the datakeys at NewSelectedIndex and set it as parameter value to ObjectDataSource. After binding the grid, update the updatePanel upCities and show the modal popup. That is it. Run the application to see the action.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 7 and 8 and type the answer here:

User Comments

Title: Developer   
Name: Rahul Tiwari
Date: 4/30/2008 3:34:47 AM
Comment:
Master page Dosn't work
Title: Doesnt Work   
Name: Chetan Mulay
Date: 4/23/2008 9:07:06 AM
Comment:
The Code doesn't work.It gives a error as:This name contains Upper case Letters.
Title: Doesnt Work   
Name: Chetan Mulay
Date: 4/23/2008 9:05:40 AM
Comment:
\
\
\
\
\
\
\
\
\
Title: Sample & Download   
Name: Joe Stagner
Date: 4/16/2008 12:59:00 PM
Comment:
FYI - The download link appears to be broken and the authors on-line demo throws a SQL Server error.
Title: Master page doesn't work   
Name: Pranil
Date: 4/16/2008 12:19:22 PM
Comment:
I am also facing the same problem as Nelson. I have a master page with a treeview and four pages. On one of the pages i have a gridview where i need to popup an image on click of a column.
I tried implementing the way given here, but when the modalpopup is displayed, the page becomes large and can be scrolled down unendingly.

Any idea what is the problem?
Title: Developer   
Name: Tim
Date: 3/30/2008 4:47:50 PM
Comment:
I get a database erro when trying the the 1509_Working_with_ModalPopup_Ajax_Control example on http://bg.analysterp.com
Title: Master page doesn't work   
Name: Nelson
Date: 1/11/2008 2:15:28 PM
Comment:
I downloaded the code provided. I add a master page. I created a default2 page that used the master page. I created the events in the code behind. I ran the web application and go to the default2 page that I created. An error is received: Sys.ArgumentIndefinedException:Value cannot be undefined. Paramter name: type.
Can you reproduce this on your machine?I would like to know if the problem is my development environment or is something else. Thanks,
Nelson
Title: Master page doesn't work   
Name: Electronic Screw
Date: 1/9/2008 4:21:36 AM
Comment:
The working example I have provided (online) is infact done with MasterPage. If the page gets into a loop, a look into your code is needed.
Title: Master page doesn't work   
Name: Nelson
Date: 1/8/2008 10:43:09 AM
Comment:
I tried it with a Master Page scenario and it doesn't work. The page gets into a loop trying to do something and the scroll bar starts to go down with no limit. Do you have any idea why is this happening?






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 7/5/2008 1:37:39 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search