Datagrid SelectedIndexChanged Problem in CodeBehind

Discussion in 'ASP .Net Datagrid Control' started by Brian K. Williams, Aug 14, 2003.

  1. 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
     
    Brian K. Williams, Aug 14, 2003
    #1
    1. Advertising

  2. Brian K. Williams

    Jos Guest

    Brian K. Williams wrote:
    > 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.

    --

    Jos Branders
     
    Jos, Aug 15, 2003
    #2
    1. Advertising

  3. Brian K. Williams

    Jim Mitchell Guest

    Are you building the column at design time or run time?

    "Brian K. Williams" <> wrote in message
    news:%...
    > 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
    >
    >
     
    Jim Mitchell, Aug 16, 2003
    #3
  4. 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" <> wrote in message
    news:...
    > Brian K. Williams wrote:
    > > 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.
    >
    > --
    >
    > Jos Branders
    >
    >
     
    Brian K. Williams, Aug 17, 2003
    #4
  5. Brian K. Williams

    Jim Mitchell Guest

    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" <> wrote in message
    news:...
    > Run Time...
    > Could that make a difference?
    >
    > "Jim Mitchell" <> wrote in message
    > news:...
    > > Are you building the column at design time or run time?
    > >
    > > "Brian K. Williams" <> wrote in message
    > > news:%...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
     
    Jim Mitchell, Aug 19, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominic
    Replies:
    2
    Views:
    724
    Saravana
    Jul 4, 2003
  2. Shiva
    Replies:
    3
    Views:
    32,126
    leoykl
    Jun 14, 2012
  3. BB
    Replies:
    0
    Views:
    389
  4. TARUN

    Calling Javascript on SelectedIndexChanged from CodeBehind

    TARUN, Aug 24, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    343
    TARUN
    Aug 24, 2006
  5. Eric
    Replies:
    1
    Views:
    192
    Steve Goodyear
    May 21, 2005
Loading...

Share This Page