Why doesn't my GridView SelectedIndexChanged event fire?

jdtaylor picture jdtaylor · Dec 2, 2011 · Viewed 21.1k times · Source

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

Answer

jwheron picture jwheron · Dec 2, 2011

Don't you need a column CommandField defined as a SelectButton? Then, your markup would look something like:

<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:CommandField ShowSelectButton="true" ButtonType="Button" />
                <asp:TemplateField HeaderText="Messages Received" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="headerClass">
                    <ItemTemplate>
                        ....
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

You didn't ask about this, but I always feel compelled to mention things like these two lines:

e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D2E6F8'")
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'")

This is a code smell. It's not a bad one, but mixing JavaScript attributes with VB/C# code is a habit you should break out of now. If you need to do something like this, add a CssClass property to your GridView and define those actions in CSS (or JavaScript/jQuery if CSS doesn't have enough events for you).


Edit:

Based on our discussion in comments, this looks like an inconsistency with the way the GridView can be modified. It may be related to the page/event lifecycle (somehow it's too late for ASP.NET to properly hook up the events?), and your best bet here is to switch from the SelectedIndexChanged event to SelectedIndexChanging.