Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GridView OnSelectedIndexChanged事件未触发_C#_Asp.net_Gridview - Fatal编程技术网

C# GridView OnSelectedIndexChanged事件未触发

C# GridView OnSelectedIndexChanged事件未触发,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试获取GridView的选定行,我知道我应该能够基于OnSelectedIndexChanged事件获取该信息。每当我单击该行时,事件不会触发 <asp:GridView ID="GridView1" runat="server" GridLines="None" Width="930px" CellPadding="4" ForeColor="#333333" onselectedindexchanged="GridView1_SelectedIndexChanged2

我正在尝试获取
GridView
的选定行,我知道我应该能够基于
OnSelectedIndexChanged
事件获取该信息。每当我单击该行时,事件不会触发

<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);

}

受保护的void GridView1\u SelectedIndexChanged2(对象发送方,事件参数e)
{
//字符串company=GridView1。SelectedRow.Cells[0]。文本;
重定向(“Client_View.aspx”,false);
}

在此方面的任何帮助都将不胜感激。我看不到任何代码会将其引用重置为我可以看到的另一个事件

您可能需要将自定义事件连接到控件。在代码隐藏中首次创建控件时,请尝试以下操作:

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

如果您只是单击
网格视图中的行,则不会触发事件。您必须在行中单击某种按钮,这将触发
RowCommand
事件,以及
SelectedIndexChanged
事件(当然,如果您单击的行尚未选中)。它与Windows窗体DataGridView=)不完全相同

要触发事件,最简单的方法是将此属性添加到
GridView
标记中:

AutoGenerateSelectButton="True"
这将创建一个“选择”
LinkButton
,当您单击它时,它将在代码隐藏中触发
Gridview1\u SelectedIndexChanged2
事件

编辑:为了澄清,您需要在此处添加该属性:

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

根据
@jadarmel27
的建议启用选择。尝试事件初始化

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

不能单击一行并让它处理
SelectedIndexChanged
事件,这是不正确的。您只需向
RowCreated
事件添加一点代码即可

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

这个问题已经问了好几年了,我当然希望有问题的人能解决这个问题,但我也遇到了同样的问题,多亏了一位回答者,我才知道问题出在哪里


检查Gridview中的实际按钮行,确保按钮字段中有
CommandName=“Select”
。由于某些原因,未添加通常自动输入的代码。

如果在选定的索引更改方法中有回发代码,则应假启用EventValidation

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


不幸的是,这并不能解决问题。您是否尝试查看按键事件而不是GridView的SelectedIndexChanged事件。没有。我的印象是selectedindexchange事件就是完成这项任务所需的全部。如果我从c#创建选择按钮呢?像这样gvSlots.SelectedIndexChanged+=this.gvSlots\u SelectedIndexChanged;gvSlots.AutoGenerateSelectButton=true;但我选择的索引仍然不起作用