Datagrid SelectedIndexChanged Problem in CodeBehind

B

Brian K. Williams

I am attempting to use the SelectedIndexChanged to detect when a row has
been selected. I have seen many examples where they are writing all the code
on the ASPX page.

When I attempt to use the SelectedIndexChanged on the Code Behind it never
fires.

////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////

The CodeBehind:
public void dbGridPlayers_SelectedIndexChanged(object sender,
System.EventArgs e){

playerInfo.Text = dbGridPlayers.SelectedItem.Cells[1].Text;

}

////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////

The APSX Code:

<asp:DataGrid id="dbGridPlayers" runat="server"
OnSelectedIndexChanged="dbGridPlayers_SelectedIndexChanged"
DataKeyField="PlayerID" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="3" CellSpacing="0"
Width="100%">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066" CssClass="ItemRow"
Font-Name="Arial" Font-Size="9"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#006699" Font-Name="Arial" Font-Size="10"></HeaderStyle>
<FooterStyle ForeColor="#000066"
BackColor="White"></FooterStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066"
BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>


Thanks for any help.
Brian K. Williams
 
J

Jos

Brian said:
I am attempting to use the SelectedIndexChanged to detect when a row
has been selected. I have seen many examples where they are writing
all the code on the ASPX page.

When I attempt to use the SelectedIndexChanged on the Code Behind it
never fires.

////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////

The CodeBehind:
public void dbGridPlayers_SelectedIndexChanged(object sender,
System.EventArgs e){

playerInfo.Text = dbGridPlayers.SelectedItem.Cells[1].Text;

}

////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////

The APSX Code:

<asp:DataGrid id="dbGridPlayers" runat="server"
OnSelectedIndexChanged="dbGridPlayers_SelectedIndexChanged"
DataKeyField="PlayerID" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="3" CellSpacing="0"
Width="100%">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066" CssClass="ItemRow"
Font-Name="Arial" Font-Size="9"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#006699" Font-Name="Arial" Font-Size="10"></HeaderStyle>
<FooterStyle ForeColor="#000066"
BackColor="White"></FooterStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066"
BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>


Thanks for any help.
Brian K. Williams

You're supposed to have a Button or LinkButton in your grid with
CommandName="Select".
Clicking this button will fire the SelectedIndexChanged event.
 
B

Brian K. Williams

It seems to me that if the SelectedIndexChanged event is available when the
code is in the ASPX, there must be a way to expose this event for use in a
code behind.

Thanks
Brian K. Williams


Jos said:
Brian said:
I am attempting to use the SelectedIndexChanged to detect when a row
has been selected. I have seen many examples where they are writing
all the code on the ASPX page.

When I attempt to use the SelectedIndexChanged on the Code Behind it
never fires.
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////

The CodeBehind:
public void dbGridPlayers_SelectedIndexChanged(object sender,
System.EventArgs e){

playerInfo.Text = dbGridPlayers.SelectedItem.Cells[1].Text;

}
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////

The APSX Code:

<asp:DataGrid id="dbGridPlayers" runat="server"
OnSelectedIndexChanged="dbGridPlayers_SelectedIndexChanged"
DataKeyField="PlayerID" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="3" CellSpacing="0"
Width="100%">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066" CssClass="ItemRow"
Font-Name="Arial" Font-Size="9"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#006699" Font-Name="Arial" Font-Size="10"></HeaderStyle>
<FooterStyle ForeColor="#000066"
BackColor="White"></FooterStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066"
BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>


Thanks for any help.
Brian K. Williams

You're supposed to have a Button or LinkButton in your grid with
CommandName="Select".
Clicking this button will fire the SelectedIndexChanged event.
 
J

Jim Mitchell

I have had some similar trouble. My problem seemed to have something to do
with the order things were firing.....

In otherwords, on postback the pageload seemed to fire first, setting the
selected index back to 0. Then the dbGrid_SelectedIndexChanged event would
fire and show me the value in the first line.

Sorry if I am wasting your time, but do you see "RAT" in the textbox if you
use the following....

If so, the event is firing and you have a different problem.

The CodeBehind:
public void dbGridPlayers_SelectedIndexChanged(object sender,
System.EventArgs e){

playerInfo.Text = "RAT"
}



Brian K. Williams said:
Run Time...
Could that make a difference?

Jim Mitchell said:
Are you building the column at design time or run time?

the
code
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////

The CodeBehind:
public void dbGridPlayers_SelectedIndexChanged(object sender,
System.EventArgs e){

playerInfo.Text = dbGridPlayers.SelectedItem.Cells[1].Text;

}
////////////////////////////////////////////////////////////////////////////
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top