Retrieving values from GridClientSelectColumn

14 posts, 0 answers
  1. Jack
    Jack avatar
    6 posts
    Member since:
    Jun 2008

    Posted 26 Jun 2008 Link to this post

    Hi,

    I have a GridClientSelectColumn in a RadGrid. When a client selects a row, I would like to retrieve a value from the selected row and save it to a variable. I have tried using the ItemCreated, ItemCommand, and ItemEvent. So far I've had no luck... any suggestions would be appreciated.

    Thanks,

    Jack   
  2. Richard
    Richard avatar
    60 posts
    Member since:
    Feb 2008

    Posted 26 Jun 2008 Link to this post

    Hi Jack,

    You can handle this client side using the ClientSettings-ClientEvents-OnRowSelected event, or server side using the OnSelectedIndexChanged event

    Richard
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Jun 2008 Link to this post

    Hi Jack,

    You can also try the following code snippet in the SelectedIndexChanged event.

    CS:
     protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)  
        {  
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)  
            {  
                if (item.Selected)  
                {  
                    string strtxt = item["columnUniqueName"].Text.ToString();  
     
                }  
            }  
        } 


    Thanks
    Princy.
  5. Acadia
    Acadia avatar
    203 posts
    Member since:
    May 2005

    Posted 27 Aug 2008 Link to this post

    I tried using the OnSelectedIndexChanged event for my rad grid and the event never fires when I click the checkbox (took it through debugger).  Any idea why?  It seems pretty straight forward. 

    Here is my grid with the GridClientSelectColumn:

    <telerik:RadGrid ID="rgJ" runat="server" Skin="WebBlue" AutoGenerateColumns="False"

    GridLines="None" AllowPaging="True" PageSize="15" AllowFilteringByColumn="True"

    AllowSorting="True" Width="98%" AllowMultiRowSelection="false">

    <MasterTableView CommandItemDisplay="None">

    <Columns>

    <telerik:GridClientSelectColumn HeaderText="Select" UniqueName="SELECT_ROW" >

    <HeaderStyle HorizontalAlign="Left" Width="40px"/>

    <ItemStyle HorizontalAlign="Center" Width="40px" />

    </telerik:GridClientSelectColumn>

    <telerik:GridBoundColumn DataField="JOB_CODE" HeaderText="Job Code" UniqueName="JOB_CODE"

    HeaderStyle-Wrap="false" ItemStyle-Wrap="false" ReadOnly="true">

    <HeaderStyle HorizontalAlign="Left" Width="100px" />

    <ItemStyle HorizontalAlign="Right" Width="100px" />

    </telerik:GridBoundColumn>

    <telerik:GridBoundColumn DataField="JOB_NAME" HeaderText="Job Name" UniqueName="JOB_NAME"

    HeaderStyle-Wrap="false" ItemStyle-Wrap="True" ReadOnly="true">

    <HeaderStyle HorizontalAlign="Left" Width="150px" />

    <ItemStyle HorizontalAlign="Center" Width="150px" Wrap="true" />

    </telerik:GridBoundColumn>

    </Columns>

    <ExpandCollapseColumn Resizable="False" Visible="False">

    <HeaderStyle Width="20px" />

    </ExpandCollapseColumn>

    </MasterTableView>

    <ClientSettings>

    <Scrolling AllowScroll="True" UseStaticHeaders="True" />

    <Selecting AllowRowSelect="True" />

    </ClientSettings>

    </telerik:RadGrid>



    And my vb.net code behind handler:

    Private

    Sub rgJ_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rgJ.SelectedIndexChanged

    Dim JobNumName As String = ""

    For Each item As GridDataItem In rgJ.MasterTableView.Items

    If (item.Selected) Then

    JobNumName = item(

    "JOB_NAME").Text.ToString & "-->" & item("JOB_CODE").Text.ToString

    End If

    Next

    End Sub


    There really isn't much to it I just can't get the serverside sub to fire when I click the checkbox of a row i n the gird.  Any help would be greatly appreciated.

    Thanks,

  6. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 27 Aug 2008 Link to this post

    Hi Acadia,

    Through the checkboxes inside a GridClientSelectColumn you perform pure client-side selection. Therefore it is expected that the SelectedIndexChanged server event is not raised since there is no postback to the server.

    If you would like to have your checkbox selection "connected" to some server-side logic, consider the solution presented in this online demo of RadGrid:

    http://demos.telerik.com/ASPNET/Prometheus/Grid/Examples/Programming/SelectRowWithCheckBox/DefaultCS.aspx

    Best regards,
    Stephen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. Richard
    Richard avatar
    60 posts
    Member since:
    Feb 2008

    Posted 27 Aug 2008 Link to this post

    You should be able to just set in your ClientSettings EnablePostBackOnRowClick="true"
  8. Acadia
    Acadia avatar
    203 posts
    Member since:
    May 2005

    Posted 27 Aug 2008 Link to this post

    I ended up trying it this way and I'm stumped as to why my serverside event easily recognizes the JOB_NUM and JOB_NAME items but cannot detect that the checkbox (Templated Column) is checked.

    Here is my simple grid:

    <

    telerik:RadGrid ID="rgJ" runat="server" Skin="WebBlue" AutoGenerateColumns="False"

    GridLines="None" AllowPaging="True" PageSize="15" AllowFilteringByColumn="True"

    AllowSorting="True" Width="98%" AllowMultiRowSelection="false">

    <MasterTableView CommandItemDisplay="None">

    <Columns>

    <telerik:GridTemplateColumn HeaderText="Select Job" UniqueName="SELECT_ROW" AllowFiltering="False">

    <HeaderStyle HorizontalAlign="Left" Width="60px" Wrap="False" />

    <ItemStyle HorizontalAlign="Left" Width="60px" Wrap="False" />

    <ItemTemplate>

    <asp:CheckBox ID="cbJS" runat="server" />

    </ItemTemplate>

    </telerik:GridTemplateColumn>

    <telerik:GridBoundColumn DataField="JOB_CODE" HeaderText="Job Code" UniqueName="JOB_CODE"

    HeaderStyle-Wrap="false" ItemStyle-Wrap="false" ReadOnly="true">

    <HeaderStyle HorizontalAlign="Left" Width="100px" />

    <ItemStyle HorizontalAlign="Right" Width="100px" />

    </telerik:GridBoundColumn>

    <telerik:GridBoundColumn DataField="JOB_NAME" HeaderText="Job Name" UniqueName="JOB_NAME"

    HeaderStyle-Wrap="false" ItemStyle-Wrap="True" ReadOnly="true">

    <HeaderStyle HorizontalAlign="Left" Width="150px" />

    <ItemStyle HorizontalAlign="Center" Width="150px" Wrap="true" />

    </telerik:GridBoundColumn>

    </Columns>

    <ExpandCollapseColumn Resizable="False" Visible="False">

    <HeaderStyle Width="20px" />

    </ExpandCollapseColumn>

    </MasterTableView>

    <ClientSettings>

    <Scrolling AllowScroll="True" UseStaticHeaders="True" />

    <Selecting />

    </ClientSettings>

    </telerik:RadGrid>



    My user selects a row by clicking the checkbox for that row, then they clikc the OK button which posts back to the following server code.

    Protected

    Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

    Dim JobNumName As String = ""

    For Each item As GridDataItem In rgJ.Items

    Dim cbJs As CheckBox = CType(item.FindControl("cbJS"), CheckBox)

    If (cbJs.Checked) Then   'THE CODE NEVER DETECTS THAT THIS CHECKBOX IS CHECKED.  IT ALWAYS SAYS FALSE!  WHY???

    JobNumName = item(

    "JOB_NAME").Text.ToString & "-->" & item("JOB_CODE").Text.ToString

    End If

    Next

    End Sub


    Could somebody please explain to me why the code does not recognize a checked checkbox in a templated column in the grid, but instead always sees it as false.

    Thanks

  9. Richard
    Richard avatar
    60 posts
    Member since:
    Feb 2008

    Posted 27 Aug 2008 Link to this post

    If you change your block:

    <ClientSettings>

    <Scrolling AllowScroll="True" UseStaticHeaders="True" />

    <Selecting />

    </ClientSettings>

    to:

    <ClientSettings EnablePostbackOnRowClick="True" >

    <Scrolling AllowScroll="True" UseStaticHeaders="True" />

    <Selecting AllowRowSelect="True" />

    </ClientSettings>

    You should get your postback and the SelectedIndexChanged event will fire
  10. Acadia
    Acadia avatar
    203 posts
    Member since:
    May 2005

    Posted 27 Aug 2008 Link to this post

    The problem is I have decided I want the user to select a row in the grid  by clicking the checkbox in the row, then clicking the OK button and have that post back.  That's all working fine except for some strange (and frustrating) reason the code behind cannot detect a simple checkbox being checked.  I don't see the difference as far as the vb.net code is concerned between the other two items on the same row, whose values I can easily retrieve, and the state of the checkbox.

    I mean what could be wrong with this sub?  I get an instance of the checkbox from each row and I check its state (checked).  It says FALSE every time for the row that is actually checked.

    Protected Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

    Dim JobNumName As String = ""

    For Each item As GridDataItem In rgJ.Items

    Dim cbJs As CheckBox = CType(item.FindControl("cbJS"), CheckBox)

    If (cbJs.Checked) Then   'False, false, false it says

    JobNumName = item(

    "JOB_NAME").Text.ToString & "-->" & item("JOB_CODE").Text.ToString

    End If

    Next

    End Sub



  11. Richard
    Richard avatar
    60 posts
    Member since:
    Feb 2008

    Posted 27 Aug 2008 Link to this post

    I don't have time to investigate fully, but my suspicion is that with an unbound column in a grid like that, that the state of the checkboxes is not being recreated on the server.  I'd suggest using the GridClientSelectColumn, set AllowMultiRowSelection="true" in the grid, and in your ClientSettings:

    <ClientSettings EnablePostBackOnRowClick="false">
          <Selecting AllowRowSelect="true" />
    </ClientSettings>
     
    Then in your button click handler you can use the rgJ.SelectedItems collection to get the items that have been checked.

    Richard
  12. Acadia
    Acadia avatar
    203 posts
    Member since:
    May 2005

    Posted 27 Aug 2008 Link to this post

    Ok I’ll take this and run with it.  You also triggered another idea I can try.  Thanks for all your help Richard!
  13. Acadia
    Acadia avatar
    203 posts
    Member since:
    May 2005

    Posted 27 Aug 2008 Link to this post

    I ended up solving my problem using the following example:
    http://www.telerik.com/help/aspnet-ajax/grdselectingrowwithcheckbox_server.html


    Thanks to all who helped.
  14. Hessner
    Hessner avatar
    189 posts
    Member since:
    Mar 2003

    Posted 30 Aug 2008 Link to this post

    Glad you found a solution.

    I use this in codebehind:

    foreach (GridItem Item in TheGrid.SelectedItems)  
    {  
      string someId = TheGrid.Items[Item.ItemIndex].Cells[3].Text;  
    }  
     

    Here I get my id's when using: GridClientSelectColumn.

    "I have another challenge,- I want to keep the status of GridClientSelect Column on return)." - no problems here,- just missed a "wrong" databind().

    Regards,
    Hessner

  15. Catherine
    Catherine avatar
    1 posts
    Member since:
    Dec 2010

    Posted 16 Dec 2010 Link to this post

    I was getting a similar problem - Checked was always False.
    Turns out that someone's code on Page_Load was rebinding the grid on PostBack and thus clearing the checkbox values.
    Removed the unneccessary call and all was fine. Argh!

    Oh. Also have AutoPostBack="False" for the Checkbox.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017