GridView OnSelectedIndexChanged event does not fire

I'm trying to get the selected GridView row, and I know that I should get this information based on the OnSelectedIndexChanged event. Whenever I click on a line, 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 greatly appreciated. There is no code that I can see that resets the link to another event that I see.

+10
source share
6 answers

It has been several years since this question was asked, and I certainly hope that the person with the problem understood it, but I had the same problem, and thanks to one of the defendants, I found out what the problem was.

Check the actual row of buttons in the Gridview and make sure you have CommandName="Select" in the ButtonField. For some reason, the code that is usually entered automatically has not been added.

+4
source

If you just click on a row in a GridView , this will not raise an event. You should have some button in the row for a click that will trigger the RowCommand event, as well as the SelectedIndexChanged event (if the row you selected is not already selected, of course). This is not quite the same as Windows Forms DataGridView =)

The easiest way to fire an event is to add this attribute to your GridView markup:

 AutoGenerateSelectButton="True" 

This creates a โ€œSelectโ€ LinkButton that will fire the Gridview1_SelectedIndexChanged2 event in your code when you click it.

EDIT: To clarify, you need to add this attribute:

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

It is not true that you cannot click a line and handle the SelectedIndexChanged event. You just need 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 
+5
source

Perhaps you need to connect a custom event to the control. Try something like this the first time you create a control in code:

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

If you have a postback code in the selected index change method, you must false EnableEventValidation

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

Include the selection suggested by @jadarmel27 . Try event initialization

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

Source: https://habr.com/ru/post/907799/


All Articles