I have a GridView
which you can click on a row and it should call the SelectedIndexChanged
method (which updates another part of the page based on which row was selected). I have done something similar to this before and it worked, but I can't seem to get the SelectedIndexChanged
part to be called for some reason.
The page is held in a master page which has a form runat="server"
tag, and an <asp:ScriptManager>
tag
I am using e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex))
to allow the SelectedIndexChanged
to fire by clicking anywhere on the row.
To check that the code does work apart from that, I added a CommandField
with a SelectButton
and that successfully fires, but i would prefer to find a solution without having to use that.
code is below - any help would be appreciated. Thanks
GridView
:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:HiddenField runat="server" ID="hdnScrollPosition" />
<asp:GridView ID="gridMessages" runat="server" CssClass="gridView" AutoGenerateColumns="False"
AllowPaging="true" GridLines="None" PageSize="10" ShowHeader="True"
EmptyDataText="--No Messages Received--" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Messages Received" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="headerClass">
<ItemTemplate>
....
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Code-Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.gridMessages.DataSource = ...
Me.gridMessages.DataBind()
End If
End Sub
Protected Sub gridMessages_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMessages.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D2E6F8'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")
e.Row.Attributes.Add("onclick", "saveScrollPosition(); " & ClientScript.GetPostBackClientHyperlink(Me.gridMessages, "Select$" & e.Row.RowIndex))
End If
End Sub
SelectedIndexChanged
(which never fires):
Protected Sub gridMessages_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridMessages.SelectedIndexChanged
Response.Redirect("test.aspx")
End Sub