Select Event Not Firing in ASP .NET C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I have the following test code:
The page load populates a DataTable and then binds it to a GridView. This all works fine. However when I click on one of the redered buttons the GridView1_SelectedIndexChanged handler never fires. What am I doing wrong?
Thanks
Bill

<pre style="background:white; color:black; font-family:Consolas <pre style="background:white; color:black; font-family:Consolas <span style="color:blue <<span style="color:maroon asp<span style="color:blue :<span style="color:maroon GridView <span style="color:red ID<span style="color:blue =<span style="color:blue "GridView1" <span style="color:red runat<span style="color:blue =<span style="color:blue "server"
<span style="color:red onselectedindexchanged<span style="color:blue =<span style="color:blue "GridView1_SelectedIndexChanged"<span style="color:blue >
<span style="color:blue <<span style="color:maroon Columns<span style="color:blue >
<span style="color:blue <<span style="color:maroon asp<span style="color:blue :<span style="color:maroon ButtonField <span style="color:red ButtonType<span style="color:blue =<span style="color:blue "Button" <span style="color:red Text<span style="color:blue =<span style="color:blue "Remove" <span style="color:blue />
<span style="color:blue </<span style="color:maroon Columns<span style="color:blue >
<span style="color:blue </<span style="color:maroon asp<span style="color:blue :<span style="color:maroon GridView<span style="color:blue >[/code][/code]
<pre style="background:white; color:black; font-family:Consolas <span style="color:blue public <span style="color:blue partial <span style="color:blue class <span style="color:#2b91af _Default : System.Web.UI.<span style="color:#2b91af Page
{

<span style="color:blue protected <span style="color:blue void Page_Load(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:blue if (!IsPostBack)
{
<span style="color:#2b91af DataTable dt = <span style="color:blue new <span style="color:#2b91af DataTable();
dt.Columns.Add(<span style="color:blue new <span style="color:#2b91af DataColumn(<span style="color:#a31515 "String One", <span style="color:blue typeof(<span style="color:blue string)));
dt.Columns.Add(<span style="color:blue new <span style="color:#2b91af DataColumn(<span style="color:#a31515 "String Two", <span style="color:blue typeof(<span style="color:blue string)));
dt.Columns.Add(<span style="color:blue new <span style="color:#2b91af DataColumn(<span style="color:#a31515 "String Three", <span style="color:blue typeof(<span style="color:blue string)));
dt.Columns.Add(<span style="color:blue new <span style="color:#2b91af DataColumn(<span style="color:#a31515 "String Four", <span style="color:blue typeof(<span style="color:blue string)));

<span style="color:blue for (<span style="color:blue int i = 0; i < 10; i++)
{
<span style="color:#2b91af DataRow row = dt.NewRow();
<span style="color:blue string[] rowData = <span style="color:blue new <span style="color:blue string[dt.Columns.Count];
<span style="color:blue for (<span style="color:blue int j = 0; j < dt.Columns.Count; j++)
{
rowData[j] = j.ToString();
}
row.ItemArray = rowData;
dt.Rows.Add(row);
}

GridView1.DataSource = dt;
GridView1.DataBind();
Session[<span style="color:#a31515 "dataTable"] = dt;
}
}

<span style="color:blue protected <span style="color:blue void GridView1_SelectedIndexChanged(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:blue int rowIndex = GridView1.SelectedIndex;
<span style="color:#2b91af DataTable dt = (<span style="color:#2b91af DataTable)Session[<span style="color:#a31515 "DataTable"];
dt.Rows.RemoveAt(rowIndex);
GridView1.DataBind();
}[/code]
<pre style="background:white; color:black; font-family:Consolas <span style="color:blue </<span style="color:maroon asp<span style="color:blue :<span style="color:maroon GridView<span style="color:blue >[/code]

View the full article
 
Back
Top