I have created a wrapper control around the telerik RadGrid to be able to set some default settings and implement some of my own methods. Within my wrapper user control I added the property ShowEditButton and ShowDeleteButton which implement my own update / delete action. Once this property is set I programmatically add a GridButtonColumn which represents the update and/or delete button in the grid.
I add my custom action column with the following code:
| if (!IsPostBack){ |
| GridButtonColumn col = new GridButtonColumn(); |
| col.CommandName = col.Text = col.HeaderText = "Delete"; |
| col.UniqueName = "DeleteColumn"; |
| radDataGrid.Columns.Add(col); |
| } |
If I execute this code within de Load event of my wrapper the column doesn't get added at all. If I execute the code in the Init event of my wrapper the column is added but with every (ajax) postback the column is being re-added to the grid which results in having multiple delete buttons within the grid.
Implementation (Implementation of custom wrapper, DataGrid, see below):
| <myapp:DataGrid ID="grdAdminOverview" runat="server" ShowEditButton="true" ShowDeleteButton="true"> |
| <Columns> |
| <telerik:GridBoundColumn HeaderText="Firstname" DataField="firstname" /> |
| <telerik:GridBoundColumn HeaderText="Lastname" DataField="lastname" /> |
| <telerik:GridBoundColumn HeaderText="Username" DataField="username" /> |
| <telerik:GridBoundColumn HeaderText="Email" DataField="email" /> |
| <telerik:GridBoundColumn HeaderText="Isadmin" DataField="isadmin" /> |
| </Columns> |
| </myapp:DataGrid> |
Wrapper (wraps RadGrid with default settings):
| <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DataGrid.ascx.cs" Inherits="Controls.DataGrid" %> |
| <telerik:RadGrid ID="radDataGrid" runat="server" AutoGenerateColumns="false" AllowPaging="true" Height="425px" Width="750px"> |
| </telerik:RadGrid> |
As soon as I add my custom columns within the Init event of my custom control the columns are being added, but as said this results in having a column being added with each (ajax) postback. To prevent this from happening I tried checking if the column is already added (also in init event) using: radDataGrid.Columns.FindByUniqueNameSafe("DeleteColumn") but this always returns null.
Am I doing something wrong here?