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="XmlDataSourc e1" 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:TextB ox>
</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:TextB ox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# XPath("course_duration")%> '></asp:La bel>
</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_r ow" />
</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.Regis terClientS criptBlock (Page.GetT ype, "Highlight", gridScript1)
Me.Page.ClientScript.Regis terClientS criptBlock (Page.GetT ype, "UnHighlight", gridScript2)
End Sub
Protected Sub GridView1_RowDataBound(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) Handles GridView1.RowDataBound
With e.Row
.Attributes.Add("onMouseOv er", "Highlight(this);this.styl e.cursor=' hand'")
.Attributes.Add("onMouseOu t", "UnHighlight(this)")
.Attributes.Add("onclick", "window.open('course_outli ne.aspx?co urseid=" & .Cells(0).Text & "')")
End With
End If
End Sub
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -----
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
<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:TextB
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# XPath("course_title")%>'><
</ItemTemplate>
<ItemStyle Width="240px" />
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextB
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# XPath("course_duration")%>
</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_r
</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=
Dim gridScript2 As String = "<script language='" & "javascript" & "'> function UnHighlight(row) { row.style.backgroundColor=
Me.Page.ClientScript.Regis
Me.Page.ClientScript.Regis
End Sub
Protected Sub GridView1_RowDataBound(ByV
With e.Row
.Attributes.Add("onMouseOv
.Attributes.Add("onMouseOu
.Attributes.Add("onclick",
End With
End If
End Sub
--------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wing