Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:54:24+00:00 2026-05-27T05:54:24+00:00

I have a GridView which you can click on a row and it should

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:54:24+00:00Added an answer on May 27, 2026 at 5:54 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Gridview in my page Which I can Select each row i
I have GridView which I can select a row. I then have a button
want to have a Hyperlink-Button in a gridView in which I can display a
I have a gridview which is databound, I want to dynamically add a row
I have a GridView which I am populating by calling a method to return
I have a GridView in which each row has a custom view. The grid
I have a GridView containing records from a table which can be deleted or
I have one web part (the provider) which displays insurance claims in a gridview.
A typical situation: In my GridView control, I have a Footer row which contains
I have a GridView on my page. I want to set a cell click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.