Hi how can I create gridtemplatecolumn in code behind. currently I'm creating my grid purely on codebehind. There where no problem using bound column and dropdown column but I need to create a template column which at the moment I dont know how to instantiate like asp checkbox or a label. Any information would be much appreciated.
Thank in advance,
RJ
Thank in advance,
RJ
3 Answers, 1 is accepted
0
Accepted
Shinu
Top achievements
Rank 2
answered on 04 Jan 2010, 06:47 AM
Hello,
Checkout the part "Creating template columns programmatically" in the following documentation which describes how to add GridTemplateColumn dynamically.
Programmatic creation
Hope this helps,
Shinu.
Checkout the part "Creating template columns programmatically" in the following documentation which describes how to add GridTemplateColumn dynamically.
Programmatic creation
Hope this helps,
Shinu.
0
RJ
Top achievements
Rank 1
answered on 04 Jan 2010, 03:58 PM
Hi Shinu, I was able to create my template checkbox but using the template checkbox generated using code behind gives me error when I use it to delete items that are checked.
Heres my old code using aspx columns:
I replaced my static column and replaced it with this one
and created addhandler on itemcreated
but page blows after clicking on delete
What did I miss? or maybe where can I place the CheckChanged?
I already tried it also on ItemDatabound but to no avail.
Thanks again,
RJ
Heres my old code using aspx columns:
| <rad:GridTemplateColumn HeaderText="Delete" Groupable="False" UniqueName="TemplateColumn" HeaderImageUrl="images/delete.gif"> |
| <ItemStyle HorizontalAlign="Center" Width="35px" CssClass="ResizeItem" VerticalAlign="Middle"></ItemStyle> |
| <HeaderStyle Font-Size="XX-Small" Font-Underline="True" Font-Names="Arial" HorizontalAlign="Center" |
| ForeColor="Blue" Width="23px" VerticalAlign="Middle"></HeaderStyle> |
| <ItemTemplate> |
| <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="false" OnCheckedChanged="CheckChanged"></asp:CheckBox> |
| </ItemTemplate> |
| </rad:GridTemplateColumn> |
| Private ReadOnly Property CustomersChecked() As Hashtable |
| Get |
| Dim res As Object = ViewState("_cc") |
| If res Is Nothing Then |
| res = New Hashtable |
| ViewState("_cc") = res |
| End If |
| Return CType(res, Hashtable) |
| End Get |
| End Property |
| Protected Sub CheckChanged(ByVal sender As Object, ByVal e As System.EventArgs) |
| Dim box As CheckBox = CType(sender, CheckBox) |
| Dim item As GridDataItem = CType(box.NamingContainer, GridDataItem) |
| Dim target As Hashtable = Nothing |
| target = CustomersChecked |
| If box.Checked Then |
| DeletedItems &= item.GetDataKeyValue("ID").ToString & "," |
| target(item.GetDataKeyValue("ID").ToString) = True |
| Else |
| DeletedItems = "" |
| target(item.GetDataKeyValue("ID").ToString) = Nothing |
| End If |
| End Sub |
| Private Sub RadGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles CensusGrid.ItemDataBound |
| Try |
| If TypeOf e.Item Is GridDataItem Then |
| Dim item As GridDataItem = e.Item |
| Dim box As CheckBox = CType(item.FindControl("CheckBox1"), CheckBox) |
| Dim isChecked As Object = Nothing |
| isChecked = CustomersChecked(item("ID").Text) |
| box.Checked = False |
| End If |
| Catch ex As Exception |
| End Try |
| End Sub |
I replaced my static column and replaced it with this one
| 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) |
| RadGrid.MasterTableView.Columns.Add(templateColumn) |
| Catch ex As Exception |
| End Try |
| 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 |
| 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 |
| Private Sub RadGrid_ItemCreated(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles CensusGrid.ItemCreated |
| If (TypeOf e.Item Is GridDataItem) Then |
| Dim ck As CheckBox = CType(e.Item.FindControl("CheckBox1"), CheckBox) |
| AddHandler ck.CheckedChanged, AddressOf CheckChanged |
| End If |
| End Sub |
| Server Error in '/Sample' Application. |
| -------------------------------------------------------------------------------- |
| Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. |
I already tried it also on ItemDatabound but to no avail.
Thanks again,
RJ
0
RJ
Top achievements
Rank 1
answered on 04 Jan 2010, 07:45 PM
I was able to make page not to blow by creating all columns in code behind but then again new problem came up. My checkbox gridtemplatecolumn disappears when I click on delete. Just created a new thread for this one. Thanks Shinu.