Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" >

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...