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

GridCheckBoxColumn AutoPostBack

2 Answers 76 Views
Grid
This is a migrated thread and some comments may be shown as answers.
small_steps
Top achievements
Rank 1
small_steps asked on 09 Oct 2013, 02:23 PM
I have enabled the GridCheckBoxColumn's checkboxes during the ItemDataBound event.  Thus the client can click the (otherwise disabled) checkboxes.  Although I have set the AutoPostBack=true and added a handler for the CheckedChanged event I receive no postback.
Please advice (v2013.2.717.35).

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"  Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
 
        <telerik:RadGrid ID="RadGrid1" Width="300px" AllowPaging="True" PageSize="10"
            runat="server" AllowSorting="true" GridLines="None" AutoGenerateColumns = "False"
            AllowMultiRowSelection="True">
            <ClientSettings ColumnsReorderMethod="Reorder" EnablePostBackOnRowClick="true">
                <Selecting AllowRowSelect="True" />
            </ClientSettings>
            <MasterTableView Width="100%" EditMode="InPlace">
                <RowIndicatorColumn Visible="False"> </RowIndicatorColumn>
                <ExpandCollapseColumn Created="True"> </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridCheckBoxColumn HeaderText="Select" DataField="IsSelected" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn HeaderText="Item name" DataField="ItemID"></telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
        </telerik:RadGrid>
 
    </div>
    </form>
</body>
</html>


Imports System.Data
Imports Telerik.Web.UI
 
Partial Class _Default
    Inherits System.Web.UI.Page
    
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            MakeDatasource()
        End If
    End Sub
 
    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        RadGrid1.DataSource = Session("ItemData")
    End Sub
 
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If TypeOf e.Item Is GridEditableItem Then
            Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Dim chk As CheckBox = DirectCast(item("IsSelected").Controls(0), CheckBox)
            chk.Enabled = True
            chk.AutoPostBack = True
            AddHandler chk.CheckedChanged, AddressOf UpdateEditedItem
        End If
    End Sub
 
    Protected Sub UpdateEditedItem(ByVal sender As Object, ByVal e As EventArgs)
        Dim chk As CheckBox = DirectCast(sender, CheckBox)
        Dim item As GridEditableItem = DirectCast(chk.NamingContainer, GridEditableItem)
 
        Dim values As New Hashtable()
        item.ExtractValues(values)
 
        For Each rowView As DataRowView In mData
            Dim row As DataRow = rowView.Row
            If values("ItemID") = row("ItemID").ToString() Then
                row("IsSelected") = Not row("IsSelected")
                Exit For
            End If
        Next
        Session("ItemData") = mData
        RadGrid1.Rebind()
    End Sub
 
    Private Sub MakeDatasource()
        Dim dt As New DataTable
        dt.Columns.Add("IsSelected")
        dt.Columns.Add("ItemID")
 
        Dim dv As DataView = dt.DefaultView
        For i = 0 To 19
            Dim newRow As DataRowView = dv.AddNew()
            newRow("IsSelected") = (i Mod 2 = 0) 'every other row is selected
            newRow("ItemID") = "item " + i.ToString()
            newRow.EndEdit()
        Next
        Session("ItemData") = dv
    End Sub
End Class

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 10 Oct 2013, 05:31 AM
Hi ,

Please move the code from OnItemDataBound into OnItemCreated event of the radgrid and please set the UniqueName of the CheckBoxColumn.

ASPX:
<telerik:GridCheckBoxColumn HeaderText="Select" DataField="IsSelected" UniqueName="IsSelected">
</telerik:GridCheckBoxColumn>

VB:
Protected Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim chk As CheckBox = DirectCast(item("IsSelected").Controls(0), CheckBox)
        chk.Enabled = True
        chk.AutoPostBack = True    
        AddHandler chk.CheckedChanged, AddressOf UpdateEditedItem
    End If
End Sub

Thanks,
Princy
0
small_steps
Top achievements
Rank 1
answered on 10 Oct 2013, 11:36 AM
Thanks, that works.
Tags
Grid
Asked by
small_steps
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
small_steps
Top achievements
Rank 1
Share this question
or