Search
Categories
  ASP.NET
Visual C#
Visual Basic
.NET Framework
ADO.NET
AJAX
LINQ
Java
 
Tutorial Rating
 
2.5 out of 5


Total Votes: 62
         
1 2 3 4 5
 




Display GridView Row Details using ASP.NET AJAX Popup Control

Author: Waqas Anwar - Posted Date: 16-July-2009 - Category: AJAX   
Bookmark and Share
When you display records using ASP.NET GridView control it displays all the columns on the page and often you want to display only few columns in GridView due to the available space on the page. You can easily achieve this functionality by adding your own columns declaratively in the GridView control. But if you want to display other columns in a popup window for each row in the GridView control you need to use AJAX Popup Control with the GridView. In this tutorial I will show you how you can display GridView row details using ASP.NET AJAX Popup control extender.
 
ASP.NET AJAX Popup Control Extender


To start this tutorial, create a new website in Visual Studio 2008 and add ScriptManager control on the page. This control is required to use any AJAX functionality on ASP.NET page.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

Next you need to add GridView control on the page that has AutoGenerateColumns property set to false to stop ASP.NET to generate columns automatically. You also need to attach OnRowCreated event handler with the grid which I will use later in this tutorial to attach some JavaScript events with a magnify icon to display popup window on mouse over. Following is the complete markup of the GridView control.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
   BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"
   CellPadding="3" Font-Names="Verdana" Font-Size="10pt"
   onrowcreated="GridView1_RowCreated">

   <Columns>
      <asp:BoundField DataField="ProductID" HeaderText="Product ID" />
      <asp:BoundField DataField="ProductName" HeaderText="Product Name" />

      <asp:TemplateField ItemStyle-Width="40" ItemStyle-HorizontalAlign="Right">
         <ItemTemplate>

            <asp:Image ID="Image1" runat="server" ImageUrl="~/images/magnify.gif" />

            <ajax:PopupControlExtender ID="PopupControlExtender1" runat="server"
               PopupControlID="Panel1"
               TargetControlID="Image1"
               DynamicContextKey='<%# Eval("ProductID") %>'
               DynamicControlID="Panel1"
               DynamicServiceMethod="GetDynamicContent" Position="Bottom">
            </ajax:PopupControlExtender>

         </ItemTemplate>

      </asp:TemplateField>

   </Columns>
   <HeaderStyle BackColor="#336699" ForeColor="White" />
</asp:GridView>

The most important thing in the above markup is the TemplateField column definition in which I have used an image control to display magnify icon and ASP.NET AJAX Popup Control Extender.

The PopupControlID property needs the id of the control you want to use as a popup window with AJAX Popup Control in this case I have used Panel control.

The TargetControlID property needs the id of the control that will display the popup window in this case I have used Image1 control that shows magnify icon in the GridView column.

The DynamicServiceMethod property needs the name of the web service method you want to call for every popup window to load additional details from the database.

The DynamicContextKey property needs the dynamic value you want to pass to the dynamic web service method. In this example I am passing ProductID to load more details of the product in the popup window dynamically.

In the end you need a Panel control on the page which will be used as a popup window by the PopupControlExtender control.

<asp:Panel ID="Panel1" runat="server"> </asp:Panel>
Now I am moving to the code behind file of the ASP.NET page that has Page Load event to bind data with the GridView control as shown below:
protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      LoadData();
   }
}

private void LoadData()
{
   string constr = "ServerTestServer;Database=SampleDatabase;uid=test;pwd=test;";
   string query = "SELECT ProductID, ProductName FROM Products";

   SqlDataAdapter da = new SqlDataAdapter(query, constr);
   DataTable table = new DataTable();

   da.Fill(table);

   GridView1.DataSource = table;
   GridView1.DataBind();
}

Next you need the OnRowCreated event definition that will perform two operations during the creation of every data row of the GridView control. First it will find the Popup Extender Control using the FindControl method and will add a unique behavior id with each popup control. Secondly it will find Image control in every row and will attach showPopup() and hidePopup() script with the image onmouseover and onmouseout events. This is required to display popup window on mouse over and hide popup window on mouse out automatically.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      PopupControlExtender pce = e.Row.FindControl("PopupControlExtender1") as PopupControlExtender;
     
      string behaviorID = "pce_" + e.Row.RowIndex;
      pce.BehaviorID = behaviorID;

      Image img = (Image)e.Row.FindControl("Image1");

      string OnMouseOverScript = string.Format("$find('{0}').showPopup();", behaviorID);
      string OnMouseOutScript = string.Format("$find('{0}').hidePopup();", behaviorID);

      img.Attributes.Add("onmouseover", OnMouseOverScript);
      img.Attributes.Add("onmouseout", OnMouseOutScript);
   }
}

In the end, you need the definition of GetDynamicContent service method that will be called by every ASP.NET AJAX Popup Control Extender to load dynamic data from the database and to display additional details in the popup window. The first few lines in the method below are loading additional columns from the Products table in database. The StringBuilder object is created to build html string for the tabular output in the popup window. You can build any type of html for display such as bullets, paragraphs, links etc.
[System.Web.Services.WebMethodAttribute(),
   System.Web.Script.Services.ScriptMethodAttribute()]
public static string GetDynamicContent(string contextKey)
{
   string constr = "ServerTestServer;Database=SampleDatabase;uid=test;pwd=test;";
   string query = "SELECT UnitPrice, UnitsInStock, Description FROM Products WHERE ProductID = " + contextKey;

   SqlDataAdapter da = new SqlDataAdapter(query, constr);
   DataTable table = new DataTable();

   da.Fill(table);

   StringBuilder b = new StringBuilder();

   b.Append("<table style='background-color:#f3f3f3; border: #336699 3px solid; ");
   b.Append("width:350px; font-size:10pt; font-family:Verdana;' cellspacing='0' cellpadding='3'>");
  
   b.Append("<tr><td colspan='3' style='background-color:#336699; color:white;'>");
   b.Append("<b>Product Details</b>"); b.Append("</td></tr>");
   b.Append("<tr><td style='width:80px;'><b>Unit Price</b></td>");
   b.Append("<td style='width:80px;'><b>Stock</b></td>");
   b.Append("<td><b>Description</b></td></tr>");

   b.Append("<tr>");
   b.Append("<td>$" + table.Rows[0]["UnitPrice"].ToString() + "</td>");
   b.Append("<td>" + table.Rows[0]["UnitsInStock"].ToString() + "</td>");
   b.Append("<td>" + table.Rows[0]["Description"].ToString() + "</td>");
   b.Append("</tr>");

   b.Append("</table>");

   return b.ToString();
}

If you have implemented everything in this tutorial properly you will see the output similar to the figure shown in the start of this tutorial. Feel free to experiment with this tutorial and customize the output and data according to your own requirements.



RELATED TUTORIALS

  Using ASP.NET AJAX ModelPopupExtender from Client Side
  Using ASP.NET AJAX UpdateProgress Control
  Using ASP.NET AJAX Accordion Control
  ASP.NET AJAX TextBox Watermark Control
  Using ASP.NET AJAX ModelPopupExtender from Server Side
  Using ASP.NET AJAX Timer Control
  Using ASP.NET AJAX HoverMenu Control with GridView
  Understanding ASP.NET AJAX UpdatePanel
  Using ASP.NET AJAX Popup Control Extender

LEAVE YOUR COMMENTS LEAVE YOUR COMMENTS
 Name (required)  
 Email (required
 Website


 
POSTED COMMENTS POSTED COMMENTS

afain nice work,sir
thanks
Posted by mehmood on Saturday, August 29, 2009

provide some live demos with examples above
Posted by shankar on Tuesday, February 23, 2010

Sir i worked on this...but i am getting error as web service call failed:500...I am not getting how to solve this..so please do help me out....
Posted by Shivaraj on Sunday, February 28, 2010

nice code
i was searching for this
thanks
Posted by swapnil on Monday, March 15, 2010

Hi,
Nice example. Only issue I am running into is that I am not able to see the popup. Is "showPopup" and "hidePopup" javascript functions that we have to write?

can you send me this sample code?

Thanks
Posted by Uday on Monday, March 15, 2010

Hi,
Nice example. Only issue I am running into is that I am not able to see the popup. Is "showPopup" and "hidePopup" javascript functions that we have to write?

can you send me this sample code?

Thanks
Posted by Uday on Monday, March 15, 2010

I got it working now. Thank you.
Posted by Uday on Monday, March 15, 2010

You can do that exact same thing with 2 lines of Div tags and 4 lines of java.
Posted by Mike on Wednesday, April 07, 2010

I am using this exact method to display extended details of the data row items when the mouse hovers over an image in the row. I am having a weird issue though. My gridview is in an Update panel and after a postback occurs I get varying results in the panel. Sometimes the panel works, sometime I get details that are related to a different record. Any thoughts on what might be causing this?
Posted by mfitz on Thursday, April 15, 2010

excellent code , Tutorial.
Waqas You are really rock........
Posted by Muhammad Fahad Basit on Sunday, April 18, 2010

Thanks Fahad for your comments.
Posted by Waqas Anwar on Sunday, April 18, 2010

Can you replace that table with detailsview in panel1?
Posted by Tim on Monday, May 17, 2010

Look I have this problem !!
Error 3 Le type 'System.Web.Script.Services.ScriptMethodAttribute' existe dans 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\1.0.61025.0__31bf3856ad364e35\System.Web.Extensions.dll' et dans 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll' C:\Documents and Settings\z\Mes documents\Visual Studio 2005\WebSites\AJAX\Default.aspx.cs 62 31 C:\...\AJAX\
Posted by zoro hero on Wednesday, July 14, 2010

Hi,
Nice example. Only issue I am running into is that I am not able to see the popup. Is "showPopup" and "hidePopup" javascript functions that we have to write?

can you send me this sample code?

Thanks
Posted by rajendra kumar v on Wednesday, August 18, 2010

This code works perfectly. Here are some tips to get this working:
1. Need these using statements

using using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using AjaxControlToolkit;
using System.Text;

2. It works fine in ASP.NET 2.0
Posted by Scott on Wednesday, September 01, 2010

 


 
 
 

Categories

My Portfolio

Website Links


Copyright @ 2009 EzzyLearning.com