[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2640
  • Last Modified:

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
-------------------------------------------------------------------------------------------------------------------------
0
WingYip
Asked:
WingYip
1 Solution
 
Dustin HopkinsCommented:
You could use a hyperlink column.
 <asp:HyperLinkField HeaderText="course_id" DataTextField="course_id" DataNavigateUrlFields="course_id" DataNavigateUrlFormatString="course_outline.aspx?courseid={0}" >
</asp:HyperLinkField>

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 window.open
0
 
WingYipAuthor Commented:
Thanks very much

Wing
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now