Click here to Skip to main content
15,880,469 members
Articles / Web Development / ASP.NET

Popup window for the GridView

Rate me:
Please Sign up or sign in to vote.
4.70/5 (21 votes)
11 Jul 2006CPOL2 min read 206.9K   3.4K   53   17
How to create a popup window for the GridView control.

Sample Image - gridview_popup.jpg

Introduction

Sometimes it is useful to display not all the information for a data record in the GridView. For example, to consume less space for the GridView. When the user needs the detailed information for a record, you can display it in a popup-window.

Binding a DataTable to the GridView

First, we need a GridView with some data rows. As we don’t want to use a database, we create a new DataTable on the fly and bind it to the GridView. Here is the source code for this task (in the file Default.aspx.vb):

VB
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            bindGridView()
        End If
    End Sub
    Sub bindGridView()
        Dim objDT = New DataTable("Mitarbeiter")
        objDT.Columns.Add("ID", GetType(String))
        objDT.Columns.Add("Name", GetType(String))
        objDT.Columns.Add("Tel", GetType(String))
        Dim objDR As DataRow
        '-------------------------------------------

        objDR = objDT.NewRow
        objDR("ID") = "1"
        objDR("Name") = "Florian"
        objDR("Tel") = "123"
        objDT.Rows.Add(objDR)
        '-------------------------------------------

        ' Add more rows …

        '-------------------------------------------

        GridView1.DataSource = objDT
        GridView1.DataBind()
     End Sub
End Class

Creating a template field with the hyperlink

Next, we create a template field that holds the hyperlink to the popup window as well as a picture. Here is the generated code in the file Default.aspx:

ASP.NET
<asp:GridView ID="GridView1" runat="server" 
  Style="left: 84px; position: relative; top: 50px"> 
… Here goes the template field …
</asp:GridView>

Here is the code after creating an additional template field for the hyperlink and the picture:

ASP.NET
<asp:GridView ID="GridView1" runat="server" 
     Style="left: 84px; position: relative; top: 50px">
        <Columns>
            <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <a href="javascript:openPopup('Info.aspx?id=<%# Eval("ID") %>')">
                   <img src="pics/info.gif" border=0px width=13px/></a>
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

Take a look at the hyperlink code:

HTML
"javascript:openPopup('Info.aspx?id=<%# Eval("ID") %>')"

Here, we call a JavaScript function openPopup that takes a string as a parameter. This string contains the name of the popup page (Info.aspx) and a query string with the ID from the current record. We get the ID for the current record using Eval(“id”).

The JavaScript function

Now, we add a JavaScript function to the head section of the file Default.aspx. Here is the code:

JavaScript
<script language="javascript">
function openPopup(strOpen)
{
    open(strOpen, "Info", 
         "status=1, width=300, height=200, top=100, left=300");
}
</script>

The third parameter defines how and where the popup window will appear on the screen.

The popup window

The popup window will be an ASPX-file called Info.aspx that displays some information about the selected record. Here is the code for the PageLoad event:

VB
Partial Class Info
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            lblDebug.Text = Request.QueryString("id")
            lblInfo.Text = "Here is some important info about: " + _
                           Request.QueryString("id")
        End If
    End Sub
End Class

Why???

The problem about web pages is, you don’t have enough space to display everything and everywhere. So, display only the main information in the GridView. When the user needs detailed information about a record, you can open a popup window.

License

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


Written By
Web Developer
Germany Germany
Florian works as consultant for change- and configuration management for about 7 years. In this environment he is often forced to work with unix, perl and shell scripts.

For more information about change- and configuration management (espacially Serena Dimensions) visit: www.venco.de

For video tutorials about asp.net, ajax, gridviews, ... (in german) visit: www.siore.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Avik Ghosh2228-Feb-13 20:47
Avik Ghosh2228-Feb-13 20:47 
QuestionIf statement Pin
Member 949904416-Oct-12 7:48
Member 949904416-Oct-12 7:48 
Thank you for sharing your code. It works perfect. Could i ask you to put it into an IF statement?

I would like the clickable image only to appear if a field is not null or empty. In its place it could be a blank.gif.
Questionuse button Pin
Member 348097612-Jun-12 10:56
Member 348097612-Jun-12 10:56 
Questioncan u pls convert the code to c# .. Pin
danielwinata2-Jan-08 15:52
professionaldanielwinata2-Jan-08 15:52 
AnswerRe: can u pls convert the code to c# .. Pin
Amit Agarrwal16-Apr-08 21:31
Amit Agarrwal16-Apr-08 21:31 
GeneralUsing database Pin
aluizs112-Aug-07 12:57
aluizs112-Aug-07 12:57 
GeneralI want to know that Pin
Tej's21-Jul-07 17:58
Tej's21-Jul-07 17:58 
GeneralRe: I want to know that Pin
fstrahberger21-Jul-07 21:32
fstrahberger21-Jul-07 21:32 
GeneralPopup window for the GridView Pin
Fadao6-Jun-07 8:00
professionalFadao6-Jun-07 8:00 
GeneralRe: Popup window for the GridView Pin
fstrahberger21-Jul-07 21:28
fstrahberger21-Jul-07 21:28 
Generalgridview header Pin
o5ama14-Aug-06 18:23
o5ama14-Aug-06 18:23 
AnswerRe: gridview header Pin
fstrahberger14-Aug-06 23:21
fstrahberger14-Aug-06 23:21 
GeneralRe: gridview header Pin
ayeleteric23-Nov-06 4:09
ayeleteric23-Nov-06 4:09 
GeneralThanx! Pin
Ashish Nijai3-Aug-06 4:44
Ashish Nijai3-Aug-06 4:44 
Generalgrammer Pin
StevenMcD12-Jul-06 21:24
StevenMcD12-Jul-06 21:24 
GeneralRe: grammer Pin
fstrahberger12-Jul-06 22:01
fstrahberger12-Jul-06 22:01 
GeneralReply Pin
Member 23384945-Apr-14 22:36
Member 23384945-Apr-14 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.