bluesky
bluesky

Reputation: 646

GridView OnSelectedIndexChanged event not firing

I am trying to get the selected row of the GridView, and I know that I should be able to get that information based on the OnSelectedIndexChanged event. Whenever I click on the row, the event does not fire.

<asp:GridView ID="GridView1" runat="server" GridLines="None"
  Width="930px" CellPadding="4" ForeColor="#333333" 
  onselectedindexchanged="GridView1_SelectedIndexChanged2">
  <AlternatingRowStyle BackColor="White" />
  <EditRowStyle BackColor="#2461BF" />
  <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  <RowStyle BackColor="#EFF3FB" />
  <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  <SortedAscendingCellStyle BackColor="#F5F7FB" />
  <SortedAscendingHeaderStyle BackColor="#6D95E1" />
  <SortedDescendingCellStyle BackColor="#E9EBEF" />
  <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

protected void GridView1_SelectedIndexChanged2(object sender, EventArgs e)
{
    //string company = GridView1.SelectedRow.Cells[0].Text;
    Response.Redirect("Client_View.aspx", false);

}

Any help with this would be appreciated. There is no code that I can see that resets its reference to another event, that I can see.

Upvotes: 11

Views: 44610

Answers (7)

sajawal khan Jadoon
sajawal khan Jadoon

Reputation: 153

I recently encountered this bug/error on one of my ASPX forms in Visual Studio 2022 where debugger was unable to hit the fired event. According to my experience, this strange behavior is displayed when you copy markup/code from other files. Particular controls with events attached to them cause this issue.

Steps I took (several time):

  • I re-examine the markup and code.
  • Restarted Visual Studio
  • Delete data from temp and .vs folders
  • Clean and rebuild project

Finally, I created a new form and added the controls markup manually. That's worked.

Upvotes: 0

Lord nick
Lord nick

Reputation: 56

It's been a few years since this question was asked and I certainly hope the person with the problem got it figured out but I had the same problem and thanks to one of the responders I figured out what the problem was.

Check the actual button line in the Gridview and make sure that you have the CommandName="Select" in the ButtonField. For some reason that code, which is normally entered automatically wasn't added.

Upvotes: 4

Seyed Reza Dadrezaei
Seyed Reza Dadrezaei

Reputation: 583

If you have postback code in selected index changing method you should false EnableEventValidation

  <%@ Page Title="" Language="C#" EnableEventValidation="false" MasterPageFile="~/Administration/Site.master" AutoEventWireup="true" CodeFile="CourseStatusReport.aspx.cs" Inherits="Administration_Reports_CourseStatusReport" %>

Upvotes: 2

Tim Waite
Tim Waite

Reputation: 111

It is NOT true that you can't click a row and have it handle the SelectedIndexChanged event. You just have to add a little code to the RowCreated event.

Protected Sub yourDataGrid_RowCreated(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles yourDataGrid.RowCreated
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackEventReference(Me.yourDataGrid, "Select$" & e.Row.RowIndex)
    End If
End Sub

Upvotes: 8

Mubarek
Mubarek

Reputation: 2689

Enable selection as suggested by @jadarmel27. Try event initialization

protected void Page_Init(object sender, EventArgs e)
{
   GridView1.SelectedIndexChanged += this.GridView1_SelectedIndexChanged2;
}

Upvotes: 0

Josh Darnell
Josh Darnell

Reputation: 11433

If you are just clicking on the row in the GridView, that will not fire the event. You have to have some kind of button in the row to click on, which will fire the RowCommand event, as well as the SelectedIndexChanged event (if the row you click is not already selected, of course). It's not exactly like the Windows Forms DataGridView =)

The easiest way to get the event to fire, is to add this attribute to your GridView markup:

AutoGenerateSelectButton="True"

This creates a "Select" LinkButton, which will fire the Gridview1_SelectedIndexChanged2 event in your code-behind when you click it.

EDIT: Just to clarify, this is where you need to add that attribute:

<asp:GridView ID="GridView1" runat="server" GridLines="None" 
  Width="930px" CellPadding="4" ForeColor="#333333"  
  onselectedindexchanged="GridView1_SelectedIndexChanged2"
  AutoGenerateSelectButton="True" >

Upvotes: 12

Dredj
Dredj

Reputation: 157

It's possible that you need to wire up the custom event to the control. Try something like this when first creating the control in the code-behind:

// Add event handler dynamically using C# syntax.
  GridView1.onselectedindexchanged += this.GridView1_SelectedIndexChanged2;

Upvotes: 1

Related Questions