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

[Solved] GridTemplateColumn Checkbox disappears after clicking Delete command button

2 Answers 199 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RJ
Top achievements
Rank 1
RJ asked on 04 Jan 2010, 07:40 PM
Hi I was able to create dynamic column creation using code behind but I'm having a problem when I click on delete button, my gridTemplateColumn checkbox disappears.
Heres my code:
<rad:radgrid id="RadGrid1" runat="server" Height="200px" AllowMultiRowSelection="True" ForeColor="White" AllowMultiRowEdit="True" Skin="outlook" width="985px" ItemStyle-BackColor="WhiteSmoke" AlternatingItemStyle-BackColor="White" BorderStyle="None" GridLines="Both">  
 
<AlternatingItemStyle Font-Size="Small" Font-Names="Arial Narrow" HorizontalAlign="Left" VerticalAlign="Middle" BorderStyle="None" CssClass="AlternatingItemRow" BackColor="White"></AlternatingItemStyle> 
                          
<MasterTableView DataKeyNames="ID" AllowAutomaticInserts="True" EditMode="InPlace" CommandItemDisplay="Top" AutoGenerateColumns="False">  
 
<CommandItemStyle BackColor="#FFE39A"></CommandItemStyle> 
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle> 
 
                              
<CommandItemTemplate> 
    <table class="MyCommandItemClass" width="100%">  
    <tr> 
                                        <td align="left" style="width: 75%; padding-top:4px">  
                                            <asp:Label ID="lblPN" runat="server" Text="PlanName" CssClass="basicfontbold" ForeColor="black" Font-Size="12px" Visible="true"></asp:Label> 
                                              
                                              
                                        </td> 
                                           
                                        <td align="right">  
                                        <asp:Button id="btnAdd" Runat="server" CommandName="InitInsert"  ToolTip="Add New Record" Text="Add" CssClass="ButtonGray" Visible='<%# Not RadGrid1.MasterTableView.IsItemInserted and showInsert %>' /> 
                                              
                                            <asp:Button ID="PerformInsert" runat="server" CommandName="PerformInsert"  ToolTip="Save" CssClass="ButtonRed" Text="Save" Visible='<%# RadGrid1.MasterTableView.IsItemInserted and showInsert %>'/>  
                                            <asp:Button ID="CancelInsert" runat="server" CommandName="CancelInsert" ToolTip="Cancel" CssClass="ButtonGray" Text="Cancel" Visible='<%# RadGrid1.MasterTableView.IsItemInserted and showInsert %>'/>  
                                              
                                           <asp:Button id="Linkbutton1" Runat="server" CommandName="DeleteChecked" Text="Delete" CssClass="ButtonGray" Visible='<%# Not RadGrid1.MasterTableView.IsItemInserted and showDelete %>' Enabled='<%# showDelete %>'/>  
                                          
                                          
                                              
                                        </td> 
                                                                            </tr> 
                                </table> 
                            </CommandItemTemplate> 
                            <ExpandCollapseColumn> 
                                <HeaderStyle Width="20px"></HeaderStyle> 
                            </ExpandCollapseColumn> 
                            <FooterStyle BorderStyle="None" BackColor="#FFE39A"></FooterStyle> 
                        </MasterTableView> 
                          
                        <HeaderStyle Font-Size="Small" Font-Names="Arial Narrow" Font-Bold="True" ForeColor="White" ></HeaderStyle>  
                        <SelectedItemStyle CssClass="RadGridSelectedItem"></SelectedItemStyle> 
                        <ItemStyle Font-Size="Small" Font-Names="Arial Narrow" HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle> 
                        <ClientSettings> 
                            <Selecting AllowRowSelect="True" EnableDragToSelectRows="False"></Selecting> 
                            <Resizing ResizeGridOnColumnResize="True" AllowColumnResize="True"></Resizing> 
                            <ClientEvents OnRowDeselected="RowDeselected" OnRowSelected="RowSelected" OnRowCreated="RowCreated"></ClientEvents> 
                            <Scrolling ScrollHeight="202px" AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="true" FrozenColumnsCount="5"></Scrolling> 
                        </ClientSettings> 
                    </rad:radgrid> 

Code behind
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load  
If Not IsPostBack Then  
 
Try  
                Dim boundColumn As GridBoundColumn  
                boundColumn = New GridBoundColumn()  
                RadGrid1.MasterTableView.Columns.Add(boundColumn)  
                boundColumn.HeaderText = "ID" 
                boundColumn.DataField = "ID" 
                boundColumn.UniqueName = "ID" 
                boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left  
                boundColumn.ItemStyle.VerticalAlign = VerticalAlign.Middle  
                boundColumn.ReadOnly = True 
                boundColumn.Visible = False 
            Catch ex As Exception  
 
            End Try  
 
            Try  
                Dim templateColumnName As String = "Delete" 
                Dim templateColumn As New GridTemplateColumn()  
                templateColumn.ItemTemplate = New GridCheckboxTemplate(templateColumnName)  
                templateColumn.HeaderImageUrl = "images/delete.gif" 
                templateColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center  
                templateColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left  
                templateColumn.ItemStyle.VerticalAlign = VerticalAlign.Middle  
                templateColumn.HeaderStyle.Width = Unit.Pixel(23)  
                templateColumn.ItemStyle.Width = Unit.Pixel(35)  
                RadGrid1.MasterTableView.Columns.Add(templateColumn)  
            Catch ex As Exception  
 
            End Try  


            Several more columns defined here.....

            RadGrid1 Code Binding here.......

end if  
end sub  
 
Private Class GridCheckboxTemplate  
        Implements ITemplate  
        Protected boolValue As CheckBox  
        Private colname As String  
        Public Sub New(ByVal cName As String)  
            colname = cName 
        End Sub  
        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn  
            boolValue = New CheckBox()  
            boolValue.ID = "CheckBox1" 
            AddHandler boolValue.DataBinding, AddressOf boolValue_DataBinding  
            boolValue.Enabled = True 
            boolValue.AutoPostBack = False 
            container.Controls.Add(boolValue)  
        End Sub  
        Sub boolValue_DataBinding(ByVal sender As Object, ByVal e As EventArgs)  
            Dim cBox As CheckBox = DirectCast(sender, CheckBox)  
            Dim container As GridDataItem = DirectCast(cBox.NamingContainer, GridDataItem)  
              
 
        End Sub  
          
    End Class  
 
 
Im using the checkbox column template to delete all items thats been checked.
Any feedback would be appreciated.

Thanks in advance,
RJ

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 05 Jan 2010, 05:15 AM
Hello RJ,

Try creating the Template column in the Page_Init event rather than creating them in the Page_Load event. When checking for the  Not IsPostBack condition and then adding the columns, the columns does not persist on postback. Check out the section 'Creating template columns programmatically' following help document to understand better:
Programmatic creation

Thanks
Princy.
0
RJ
Top achievements
Rank 1
answered on 05 Jan 2010, 01:23 PM
Thanks princy page init did the job but it also made some duplicates with my gridBoundColumn. Any workaround with this one?

Tags
Grid
Asked by
RJ
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
RJ
Top achievements
Rank 1
Share this question
or