Asp.net onselectedindexchanged method location

In a GridView, why should I define a select method on the same page instead of a C # file?

e.g. in index.aspx, I have

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="GridViewApp.index" %>     
<asp:GridView ID="GridView1" runat="server" 
          onselectedindexchanged="GridView1_SelectedIndexChanged" 
        DataSourceID="SqlDataSource2" AllowPaging="True" AllowSorting="True">
            <Columns>
                <asp:CommandField ShowSelectButton="True"  />
            </Columns>
        </asp:GridView>

GridView1_SelectedIndexChanged, should this method be defined in index.aspx instead of index.aspx.cs?

Error message

Compiler Error Message: CS1061: 'ASP.index_aspx' does not contain a definition for 'GridView1_SelectedIndexChanged' and no extension method 'GridView1_SelectedIndexChanged' accepting a first argument of type 'ASP.index_aspx' could be found (are you missing a using directive or an assembly reference?)

UPDATED: allow. after I clean the project, rebuild it. he is working now. What is the correct way to create / debug a project? How to clear the cache?

Appreciate your help.

+3
source share
3 answers

The method GridView1_SelectedIndexChangedmust be defined on the page containing GridView1, and make sure you declare it at leastprotected

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
   //do magic
}
+7
source

? , ? aspx , :

<%@ Page Language="C#" AutoEventWireup="True" Inherits="The Class Name in index.aspx.cs" Codebehind="index.aspx.cs" %>

, , , SelectedIndexChanged, GridView.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write("GridView1_SelectedIndexChanged");
}
+1

On the aspx page, you determine which event handler is called when that particular event occurs, and the cs file that you are actually implementing.

As in your case: on the aspx page, you determine that when SelectedIndexChangedthe event fires, the event GridView1_SelectedIndexChangedhandler that gets called, and in the cs file, you provide the impingement.

0
source

All Articles