Link to home
Start Free TrialLog in
Avatar of WingYip
WingYip

asked on

GridView click event to open page

Hi

I am new to asp.net 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?

Wing

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

GRIDVIEW CODE BEHIND------------------------------------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            FormatGrid()
        End Sub
        Protected Sub FormatGrid()
            AddMouseEventsToGrid()
        End Sub
        Protected Sub AddMouseEventsToGrid()
            Dim gridScript1 As String = "<script language='" & "javascript" & "'> function Highlight(row) { row.style.backgroundColor='AliceBlue'; } </script> "
            Dim gridScript2 As String = "<script language='" & "javascript" & "'> function UnHighlight(row) { row.style.backgroundColor='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);this.style.cursor='hand'")
                    .Attributes.Add("onMouseOut", "UnHighlight(this)")
                    .Attributes.Add("onclick", "window.open('course_outline.aspx?courseid=" & .Cells(0).Text & "')")
                End With
            End If
        End Sub
-------------------------------------------------------------------------------------------------------------------------
ASKER CERTIFIED SOLUTION
Avatar of Dustin Hopkins
Dustin Hopkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WingYip
WingYip

ASKER

Thanks very much

Wing