This is a migrated thread and some comments may be shown as answers.

Checkbox template column in radgrid

2 Answers 383 Views
Grid
This is a migrated thread and some comments may be shown as answers.
PV
Top achievements
Rank 1
PV asked on 03 Aug 2010, 07:41 AM
Hello Team,

I have searched over the forum to get the solution but the problem is quite different than what I found:

I have a template column of checkbox, and a button to submit the data bound to the checkboxes and need help to achieve the following:
1. Client side validation to check if any checkbox is selected or not upon clicking submit button.

2.  Get the list of all checkboxes and its values upon submit.

3. Persist the checked state of checked box upon paging or sorting.

I dont have clientsiderowselection but only checkbox to select the row.

Please provide pointers or sources.

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Aug 2010, 09:38 AM
Hello Prema,

I hope you have scenario like this:

ASPX:
<telerik:GridTemplateColumn>
  <ItemTemplate>
      <asp:CheckBox ID="CheckBox1"  runat="server" />
  </ItemTemplate>
</telerik:GridTemplateColumn>

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ClientClick();"
       OnClick="Button1_Click" />

1. Use the client code for validating whether any of the checkbox checked.

Java Script:
<script type="text/javascript">
    function ClientClick() {
        var count = 0;
        var grid = $find("<%=RadGrid1.ClientID %>");
        var MasterTable = grid.get_masterTableView();
        for (var i = 0; i < MasterTable.get_dataItems().length; i++) {
            var gridItemElement = MasterTable.get_dataItems()[i].findElement("CheckBox1");
            if (gridItemElement.checked) {
                count++;
                break;
            }
        }
        if (count == 0) {
            alert("Please check atleast one !");
            return false;
        }
    }
</script>

2. An easy way to get the checked griditems from code behind is iterating through the items and checking for the CheckBox Checked  status.

C#:
protected void Button1_Click(object sender, EventArgs e)
    {
      foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
            if (chk.Checked)
            {
                string value = item["ColumnUniqueName"].Text;// access the cell value using ColumnUniqueName
            }
        }
    }


3. Please refer the following documentation which explains how to persist selected rows on sorting/paging/filtering/grouping. Use the same technique to accomplish the requirement.
Persisting the selected rows server-side on sorting/paging/filtering/grouping

Save the checked row keyvalues in a session variable. In the pre-render event of the grid, traverse the rows of the grid and compare their key values to the values saved in the Session variable. Whenever you find a match, check the corresponding CheckBox in that row.

Thanks,
Princy
0
PV
Top achievements
Rank 1
answered on 04 Aug 2010, 10:52 AM
Hi Princy,

Great.. This is what exactly I was looking for..

Thank you so much.
Tags
Grid
Asked by
PV
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
PV
Top achievements
Rank 1
Share this question
or