Click here to Skip to main content
6,595,444 members and growing! (19,135 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » Samples     Intermediate License: The Code Project Open License (CPOL)

Expandable panel inside a GridView

By AmauriRodrigues

Another way to expand a detail panel inside a GridView.
C#, Javascript, .NET (.NET 2.0), ASP.NET, Ajax, Architect, Dev
Posted:17 Sep 2008
Updated:28 Sep 2008
Views:35,868
Bookmarked:125 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
29 votes for this article.
Popularity: 6.62 Rating: 4.53 out of 5
2 votes, 6.9%
1
1 vote, 3.4%
2

3
3 votes, 10.3%
4
23 votes, 79.3%
5

Introduction

A few weeks ago, a client asked me: "We need a simple listing, and we need to click and see details, inside the listing - not a pop-up - not another page - not reloading the entire page". I started to search, read, test. I found some interesting articles and samples, but not exactly what my client wanted. Then, I thought: I can merge some solutions and, maybe, satisfy the customer. The AJAX DynamicPopulate extender offers the mechanism. Let's open the sources and learn. Some interesting articles present some good ideas. Let's read and learn.

I would like to share the results of that work in this article.

You can see the sample project running (with simulated data only) here.

Background

When the AJAX DynamicPopulate extender is started, it calls a method on some page or web service, receives some results, and fills a control.

We can use a JavaScript function to start the extender, and we can capture the results and fill another control. The JavaScript function is activated when the user clicks the button, in the first column of the grid.

Using the Code

The main page has a GridView with three columns: an image that mimics a button to show and hide details, the name of the store, and the sum of the sales value.

The scenario is the Adventure Works Cycles, the fictitious large, multinational manufacturing company. The grid shows the top stores. When the user clicks a button, a panel is expanded and the top five models are displayed.

The onclick event of the image/button calls a JavaScript function (ExpandModels) with these parameters: customer ID, row of the grid view, destination panel, and the button clicked.

// Default.aspx.cs :
protected void gridStores_RowDataBound(object sender, GridViewRowEventArgs e) {
...
Image btn = gridViewRow.FindControl("imgModel") as Image;

btn.Attributes["onClick"] = String.Format(CultureInfo.InvariantCulture,
    "ExpandModels('{0}', '{1}', '{2}', '{3}');",
    contextKey,             // customerID + some data
    newRow.ClientID,        // Row of gridview, below the row that was clicked
    internalPanel.ClientID, // Destination panel, wich will receive the details
    btn.ClientID);          // The clicked button
...
}

The JavaScript function ExpandModels does some preparations, and starts the populate method of the 'behavior' dpBehaviorMod of the DynamicPopulate extender, passing the customer ID. The 'behavior' will send a callback to the WebMethod ExpandModelsService, which will build an HTML table and send it back to the browser.

// ExpandPanel.js :
var expPainelId    = '';  // ID of the internal panel where the details will be inserted
var expPainelAuxId = 'painelAux';  // Receives the results from the WebMethod

function ExpandModels(customerId, trId, painelId, buttonId) {
...
var tr = $get(trId).style;
tr.display='block';                         // Shows the panel ('table-row' for Firefox)
lastBehavior = $find('dpBehaviorMod');
expPainelId = painelId;
lastBehavior.add_populated(ShowTopModels);  // Sets the handle
lastBehavior.populate(customerId);          // Calls the WebMethod
}

<!-- Default.aspx -->
<cc1:DynamicPopulateExtender ID="dinPopMod"
                             runat="server"
                             BehaviorID="dpBehaviorMod"
                             CacheDynamicResults="false"
                             ClearContentsDuringUpdate="true"
                             EnableViewState="False"
                             ServiceMethod="ExpandModelsService"
                             TargetControlID="painelAux">
</cc1:DynamicPopulateExtender>

<asp:Panel ID="painelAux"
           runat="server"
           EnableViewState="false"
           Style="background-image:url('img/loading.gif'); 
                  background-repeat: no-repeat; display: none">
</asp:Panel>

When the extender returns, it fills the auxiliary panel 'painelAux' (indicated by the property TargetControlID), then another JavaScript function (ShowTopModels) copies the result into the destination panel. The panel ID is saved in the variable expPainelId by the function ExpandModels.

// ExpandPanel.js :
function ShowTopModels(s, e) {
    // The internal panel where the details will be inserted
    var p1 = $get(expPainelId);
    // The aux panel filled by the DynamicPopulateExtender
    var p2 = $get(expPainelAuxId);
    ...
    // Details HTML table
    p1.innerHTML = p2.innerHTML;
    ...
}

Inside the web method, the program builds a simple HTML table with the details.

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string ExpandModelsService(string contextKey) {
...
    StringBuilder sb = new StringBuilder("<table class = 'tab-mod'> \n"
    + "<tr> \n"
    + "<th>Model</th> \n"
    + "<th>Qty</th> \n"
    + "<th>Total price</th> \n"
    + "<th>Last sale</th> \n"
    + "</tr> \n",
    topModels.Count * 180);
...
    sb.Append("<tr> \n");
    sb.AppendFormat(CultureInfo.CurrentCulture,
        "<td>{0}</td> ",
        modelRow.Name);
...
    sb.Append("</table> \n");

    return sb.ToString();
}

The above code snippets were extracted from the sample project and simplified. The full source code is available for download.

Points of Interest

When the page is sent to the browser, only the main listing will be sent (stores). The models will be sent dynamically when the user clicks the button on each row. We could use another approach to load all the data during the first time and hide/show at the onclick event. That approach could cause a long time to load the entire page.

The 'loading.gif' file is pre-loaded by the panel 'painelAux', to speed the display when the user clicks the button.

I included the WebMethod ExpandModelsService inside 'Default.aspx'. If the project contains a lot of WebMethods, we can create one or more 'asmx' files to help us to organize the project.

Conclusion

If this article can help you in any form, I will be happy.

Certainly, you can find many mistakes, bugs, and not-so-good code in the sample project. I thank you in advance for tips, suggestions, and corrections.

References

There are many good articles already written on this subject:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

AmauriRodrigues


Member
Born and lives in São Paulo - Brasil.
Programmer since 1982, and in 'tailor' mode since 1995, I had the oportunity to work for many kinds of business.
Today I create Windows Forms applications, Web projects and Palm OS applications.
My experience :
Languages : Cobol, Fortran, Basic, Clipper, Turbo Pascal, Data Flex, C, C++, Turbo C++, Borland C++ Builder, C#, Javascript.
DBMS : Adabas, DBF, Paradox, MS Access, DB2, Oracle, MS SQL Server.
I Know and experienced a little : Natural, Prolog, Lisp, Assembly IBM/370, Assembly Z80, Assembly 8080, MS Visual Basic (5/6).
Occupation: Software Developer
Company: Ápex Logik D'ata
Location: Brazil Brazil

Other popular Ajax and Atlas articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 36 (Total in Forum: 36) (Refresh)FirstPrevNext
Generalhow to change the sample to link to sql server Pinmemberlinuxsuperfans6:20 12 Aug '09  
GeneralRe: how to change the sample to link to sql server PinmemberAmauriRodrigues13:35 17 Aug '09  
GeneralWhen you click on plus (+) image all of them disappear. Pinmemberrogercute200219:11 6 Aug '09  
GeneralRe: When you click on plus (+) image all of them disappear. PinmemberAmauriRodrigues13:41 17 Aug '09  
QuestionUsing XML file PinmemberMohammadALNajjar18:15 30 Apr '09  
GeneralSorting Pinmembernuno_senica11:04 30 Mar '09  
GeneralRe: Sorting PinmemberAmauriRodrigues13:51 30 Mar '09  
GeneralRe: Sorting Pinmembernuno_senica14:18 30 Mar '09  
Generalpls i am urgent regarding this Pinmemberomerqq14:40 11 Mar '09  
GeneralRe: pls i am urgent regarding this PinmemberAmauriRodrigues4:34 12 Mar '09  
Generali dont know how to use json .. Pinmemberomerqq21:24 20 Mar '09  
GeneralRe: i dont know how to use json .. [modified] PinmemberAmauriRodrigues14:13 21 Mar '09  
Questionuser control expands from gridview Pinmemberomerqq4:13 10 Mar '09  
GeneralInternal Panel Does not Display like demo PinmemberOne Excluded7:21 5 Mar '09  
GeneralRe: Internal Panel Does not Display like demo PinmemberAmauriRodrigues12:24 5 Mar '09  
GeneralRe: Internal Panel Does not Display like demo PinmemberOne Excluded9:09 6 Mar '09  
GeneralRe: Internal Panel Does not Display like demo PinmemberAmauriRodrigues9:47 6 Mar '09  
Generalhow can run this downloaded project Pinmemberanishkannan4:11 23 Jan '09  
GeneralRe: how can run this downloaded project PinmemberAmauriRodrigues12:26 5 Mar '09  
Generalquestion Pinmemberodread12:37 26 Oct '08  
GeneralRe: question PinmemberAmauriRodrigues13:21 30 Oct '08  
GeneralRe: question Pinmemberodread14:00 1 Nov '08  
GeneralWell done PinmemberRalph Willgoss14:59 28 Sep '08  
GeneralRe: Well done PinmemberAmauriRodrigues1:53 30 Sep '08  
GeneralRe: Well done Pinmembersurag19825:44 10 Mar '09  

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

PermaLink | Privacy | Terms of Use
Last Updated: 28 Sep 2008
Editor: Smitha Vijayan
Copyright 2008 by AmauriRodrigues
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project