[英]GridView OnSelectedIndexChanged event not firing
我試圖獲取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);
}
任何幫助都將不勝感激。 我可以看到沒有代碼重置其對另一個事件的引用,我可以看到。
如果您只是單擊GridView
的行,則不會觸發該事件。 您必須在行中有某種按鈕才能單擊,這將觸發RowCommand
事件以及SelectedIndexChanged
事件(如果您單擊的行當然尚未被選中)。 它不完全像Windows Forms DataGridView =)
讓事件觸發的最簡單方法是將此屬性添加到GridView
標記中:
AutoGenerateSelectButton="True"
這將創建一個“選擇” LinkButton
,當您單擊它時將觸發代碼隱藏中的Gridview1_SelectedIndexChanged2
事件。
編輯:只是為了澄清,這是你需要添加該屬性的地方:
<asp:GridView ID="GridView1" runat="server" GridLines="None"
Width="930px" CellPadding="4" ForeColor="#333333"
onselectedindexchanged="GridView1_SelectedIndexChanged2"
AutoGenerateSelectButton="True" >
不能單擊一行並讓它處理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中的實際按鈕行,並確保ButtonField中的CommandName="Select"
。 出於某種原因,通常自動輸入的代碼未添加。
如果您在選定的索引更改方法中有回發代碼,則應該為false EnableEventValidation
<%@ Page Title="" Language="C#" EnableEventValidation="false" MasterPageFile="~/Administration/Site.master" AutoEventWireup="true" CodeFile="CourseStatusReport.aspx.cs" Inherits="Administration_Reports_CourseStatusReport" %>
您可能需要將自定義事件連接到控件。 首次在代碼隱藏中創建控件時嘗試這樣的事情:
// Add event handler dynamically using C# syntax.
GridView1.onselectedindexchanged += this.GridView1_SelectedIndexChanged2;
按照@jadarmel27
建議啟用選擇。 嘗試事件初始化
protected void Page_Init(object sender, EventArgs e)
{
GridView1.SelectedIndexChanged += this.GridView1_SelectedIndexChanged2;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.