GridView click event to open page

Posted on 2007-10-13
Last Modified: 2008-01-09

I am new to 2

With a gridview control I have managed to display the columns that i want from an xml datasource.

The first column is bound column and what I would like to do is have a click event send the value of the first cell in the selected row to a new page.

I have been mucking around that with adding events using attributes.add and I am almost there (see below)but I have a small problem.  How do I load a page rather than open a new window.

I also want to know if there is an alternative to adding client side attributes (as below).  Is there a server side way to do this? Is there no server side click event to use?


GRIDVIEW HTML-----------------------------------------------------------------------
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource1" CssClass="grid_background" GridLines="None">
                    <asp:BoundField DataField="course_id" HeaderText="Course ID" SortExpression="course_id">
                        <ItemStyle Width="100px" />
                        <HeaderStyle HorizontalAlign="Left" />
                    <asp:TemplateField HeaderText="Course Title">
                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                             <asp:Label ID="Label1" runat="server" Text='<%# XPath("course_title")%>'></asp:Label>
                        <ItemStyle Width="240px" />
                        <HeaderStyle HorizontalAlign="Left" />
                    <asp:TemplateField HeaderText="Duration">
                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                             <asp:Label ID="Label1" runat="server" Text='<%# XPath("course_duration")%>'></asp:Label>
                        <ItemStyle Width="100px" />
                        <HeaderStyle HorizontalAlign="Left" />
                <RowStyle CssClass="grid_row" />
                <SelectedRowStyle CssClass="selected_row" />
                <HeaderStyle CssClass="grid_header" />
                <AlternatingRowStyle CssClass="grid_alternate_row" />

GRIDVIEW CODE BEHIND------------------------------------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub
        Protected Sub FormatGrid()
        End Sub
        Protected Sub AddMouseEventsToGrid()
            Dim gridScript1 As String = "<script language='" & "javascript" & "'> function Highlight(row) {'AliceBlue'; } </script> "
            Dim gridScript2 As String = "<script language='" & "javascript" & "'> function UnHighlight(row) {'white'; } </script> "
            Me.Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "Highlight", gridScript1)
            Me.Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "UnHighlight", gridScript2)
        End Sub

        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
                With e.Row
                    .Attributes.Add("onMouseOver", "Highlight(this);'hand'")
                    .Attributes.Add("onMouseOut", "UnHighlight(this)")
                    .Attributes.Add("onclick", "'course_outline.aspx?courseid=" & .Cells(0).Text & "')")
                End With
            End If
        End Sub
Question by:WingYip
    LVL 14

    Accepted Solution

    You could use a hyperlink column.
     <asp:HyperLinkField HeaderText="course_id" DataTextField="course_id" DataNavigateUrlFields="course_id" DataNavigateUrlFormatString="course_outline.aspx?courseid={0}" >

    or if you want to handle it client side, then to change the url instead of opening a new window you can  use location.href instead of
    LVL 1

    Author Comment

    Thanks very much


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now