Hi,
I have created a column at runtime that I need to be able to modify and add a checkbox in the header. This checkbox should enable the user to select all / unselect all items in the ultrawebgrid. If anyone knows please tell me how I can add this checkbox to the header at runtime and make this happen. I tried doing
Dim CheckAll As New CheckBox
column.HeaderItem.TemplateControl.Controls.Add(CheckAll)
but this did not work. I have to create this at Runtime since the column is also created in Runtime, all examples I found so far show how to do this in design. Any help is greatly appreciated
I'm using more or less the same code to create a checkbox in the CellItem instead of the Header. It works fine the first time it renders, but on post back the checkbox is no longer accessible when I iterate through the rows of the grid, and disappears when the page renders after the post back.
I put the code for inserting the checkbox in the OnInit event of the page, but it appears to behave the same as it did in OnLoad.
Is the checkbox not getting stored in the ViewState for the UltraWebGrid? If so, do I need to do something to ensure that it's state is maintained between post backs?
no chance
Hi Rumen
I moved the code that dynamically sets up the templated column to OnInit. I am still having the same problem. Following is the code I have written.
Imports System.DataImports Infragistics.WebUI.UltraWebGridImports Infragistics.WebUI.UltraWebNavigator
Partial Class View_Edit_Surcharge Inherits System.Web.UI.Page Protected Overrides Sub OnInit(ByVal e As EventArgs) MyBase.OnInit(e)
If (Not IsPostBack()) Then AddGridColumns() End If End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If (Not IsPostBack()) Then LoadData() End If End Sub
Protected Sub LoadData() PopulateCustomerGridView() End Sub
Protected Sub AddGridColumns() ' Create a templated column
Dim col As New TemplatedColumn(True)
Me.UltraWebGrid1.DisplayLayout.Bands(0).Columns.Add(col) col.Key = "Select" col.Header.Caption = "" Dim tempHeader As New GridHeaderTemplate() ' Set the header template. col.HeaderTemplate = tempHeader
End Sub
Private Sub PopulateFilteredCustomerGridView(ByVal custName As String, ByVal type As String, ByVal grade As String) Dim surchargeCustDs As DataSet = CType(Session.Item("surchargeCustDs"), DataSet) Dim filteredDT As DataTable = CType(Session.Item("filteredDT"), DataTable)
Dim whereClause As String = "" Dim custNameClause As String = "1=1" Dim typeClause As String = "1=1" Dim gradeClause As String = "1=1"
Try If (custName <> "") Then custNameClause = "CUSTOMER_NAME='" + custName + "'" End If If (type <> "") Then typeClause = "charge_code='" + type + "'" End If If (grade <> "") Then gradeClause = "AISIXXCODE='" + grade + "'" End If
whereClause = custNameClause + " And " + typeClause + " And " + gradeClause
filteredDT = surchargeCustDs.Tables(0).Clone
For Each tmpRow As DataRow In surchargeCustDs.Tables(0).Select(whereClause) filteredDT.ImportRow(tmpRow) Next Session.Item("isBinding") = True UltraWebGrid1.DataSource = filteredDT UltraWebGrid1.DataBind() Session.Item("isBinding") = False Session.Item("filteredDT") = filteredDT Catch ex As Exception End Try End Sub
Private Sub PopulateCustomerGridView() Dim surchargeCustDs As DataSet = CType(Session.Item("surchargeCustDs"), DataSet) Dim filteredDT As DataTable = CType(Session.Item("filteredDT"), DataTable) Dim isRefreshed As Boolean = CType(Session.Item("isRefreshed"), Boolean)
Try If (surchargeCustDs Is Nothing Or isRefreshed) Then surchargeCustDs = DataAccess.GetCustomerSurchargeDetails() Session.Item("surchargeCustDs") = surchargeCustDs Session.Item("isRefreshed") = False End If
For Each tmpRow As DataRow In surchargeCustDs.Tables(0).Rows filteredDT.ImportRow(tmpRow) Next Session.Item("isBinding") = True UltraWebGrid1.DataSource = filteredDT UltraWebGrid1.DataBind() Session.Item("isBinding") = False Session.Item("filteredDT") = filteredDT
Catch ex As Exception End Try End Sub
Protected Sub UltraWebTree1_NodeSelectionChanged(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebNavigator.WebTreeNodeEventArgs) Handles UltraWebTree1.NodeSelectionChanged
Dim uTNode As Node Dim uTSelLevel As Integer
Try 'hasToBeFocused = False uTNode = e.Node uTNode.Expand(False) uTSelLevel = uTNode.Level
Select Case uTSelLevel Case 0 PopulateCustomerGridView() Case 1 PopulateFilteredCustomerGridView(uTNode.Text, "", "") Case 2 PopulateFilteredCustomerGridView(uTNode.Parent.Text, uTNode.Text, "") Case 3 PopulateFilteredCustomerGridView(uTNode.Parent.Parent.Text, uTNode.Parent.Text, uTNode.Text) End Select Catch ex As Exception End Try
Protected Sub UltraWebGrid1_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.LayoutEventArgs) Handles UltraWebGrid1.InitializeLayout
Dim col As TemplatedColumn = DirectCast((e.Layout.Bands(0).Columns.FromKey("Select")), TemplatedColumn)
Dim tempCell As New PlaceHolderTemplate() ' Set the cell template. col.CellTemplate = tempCell
' make the checkbox the last column of band 0. col.Move(e.Layout.Bands(0).Columns.Count - 1)
Me.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "CheckBoxKey", "<script type='text/javascript'>var headerCheckBoxID = '" + col.HeaderItem.FindControl("headerCB").ClientID + "';</script>") Me.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "GridKey", "var gridID = '" + Me.UltraWebGrid1.ClientID + "';", True)
e.Layout.Bands(0).Columns.FromKey("CHARGE_TYPE").Hidden = True e.Layout.Bands(0).Columns.FromKey("CUST_ID").Hidden = True e.Layout.Bands(0).Columns.FromKey("SURCHARGE_ID").Hidden = True e.Layout.Bands(0).Columns.FromKey("FUT_SURCHARGE_ID").Hidden = True
e.Layout.Bands(0).Columns.FromKey("CUSTOMER_NUMBER").Header.Caption = "ACCOUNT" e.Layout.Bands(0).Columns.FromKey("CUSTOMER_NUMBER").Move(0) e.Layout.Bands(0).Columns.FromKey("CUSTOMER_NUMBER").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("CUSTOMER_NUMBER").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("CUSTOMER_NAME").Header.Caption = "NAME" e.Layout.Bands(0).Columns.FromKey("CUSTOMER_NAME").Move(1) e.Layout.Bands(0).Columns.FromKey("CUSTOMER_NAME").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("CUSTOMER_NAME").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("CHARGE_CODE").Header.Caption = "Charge" e.Layout.Bands(0).Columns.FromKey("CHARGE_CODE").Move(2) e.Layout.Bands(0).Columns.FromKey("CHARGE_CODE").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("CHARGE_CODE").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("AISIXXCODE").Header.Caption = "AISIXX" e.Layout.Bands(0).Columns.FromKey("AISIXXCODE").Move(3) e.Layout.Bands(0).Columns.FromKey("AISIXXCODE").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("AISIXXCODE").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("AMT").Header.Caption = "AMT" e.Layout.Bands(0).Columns.FromKey("AMT").Move(4) e.Layout.Bands(0).Columns.FromKey("AMT").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("AMT").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("AMT").CellStyle.BackColor = Drawing.Color.FromArgb(204, 255, 204) e.Layout.Bands(0).Columns.FromKey("UOM").Header.Caption = "UOM" e.Layout.Bands(0).Columns.FromKey("UOM").Move(5) e.Layout.Bands(0).Columns.FromKey("UOM").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("UOM").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("UOM").CellStyle.BackColor = Drawing.Color.FromArgb(204, 255, 204) e.Layout.Bands(0).Columns.FromKey("START_DATE").Header.Caption = "Start" e.Layout.Bands(0).Columns.FromKey("START_DATE").Move(6) e.Layout.Bands(0).Columns.FromKey("START_DATE").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("START_DATE").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("START_DATE").CellStyle.BackColor = Drawing.Color.FromArgb(204, 255, 204) e.Layout.Bands(0).Columns.FromKey("END_DATE").Header.Caption = "End" e.Layout.Bands(0).Columns.FromKey("END_DATE").Move(7) e.Layout.Bands(0).Columns.FromKey("END_DATE").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("END_DATE").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("END_DATE").CellStyle.BackColor = Drawing.Color.FromArgb(204, 255, 204) e.Layout.Bands(0).Columns.FromKey("FUT_AMT").Header.Caption = "AMT" e.Layout.Bands(0).Columns.FromKey("FUT_AMT").Move(8) e.Layout.Bands(0).Columns.FromKey("FUT_AMT").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("FUT_AMT").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("FUT_AMT").CellStyle.BackColor = Drawing.Color.FromArgb(255, 255, 153) e.Layout.Bands(0).Columns.FromKey("FUT_UOM").Header.Caption = "UOM" e.Layout.Bands(0).Columns.FromKey("FUT_UOM").Move(9) e.Layout.Bands(0).Columns.FromKey("FUT_UOM").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("FUT_UOM").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("FUT_UOM").CellStyle.BackColor = Drawing.Color.FromArgb(255, 255, 153) e.Layout.Bands(0).Columns.FromKey("FUT_START_DATE").Header.Caption = "Start" e.Layout.Bands(0).Columns.FromKey("FUT_START_DATE").Move(10) e.Layout.Bands(0).Columns.FromKey("FUT_START_DATE").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("FUT_START_DATE").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("FUT_START_DATE").CellStyle.BackColor = Drawing.Color.FromArgb(255, 255, 153) e.Layout.Bands(0).Columns.FromKey("FUT_END_DATE").Header.Caption = "End" e.Layout.Bands(0).Columns.FromKey("FUT_END_DATE").Move(11) e.Layout.Bands(0).Columns.FromKey("FUT_END_DATE").CellStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).Columns.FromKey("FUT_END_DATE").CellStyle.Font.Size = FontSize.Medium e.Layout.Bands(0).Columns.FromKey("FUT_END_DATE").CellStyle.BackColor = Drawing.Color.FromArgb(255, 255, 153) 'e.Layout.Bands(0).Columns.FromKey("Select").Move(12) 'e.Layout.Bands(0).FilterOptions.FilterRowStyle.BackColor = Drawing.Color.FromArgb(254, 254, 254)
'The boolean parameter inserts this header into viewstate, so that the grid will maintain ' it over a postback if it is not recreated.
Dim colHead As ColumnHeader For i As Integer = 0 To e.Layout.Bands(0).HeaderLayout.Count - 1 colHead = e.Layout.Bands(0).HeaderLayout(i) colHead.RowLayoutColumnInfo.OriginY = 1 Next
Dim customerGroup As New ColumnHeader(True) Dim chargeParametersGroup As New ColumnHeader(True) Dim currentPricesGroup As New ColumnHeader(True) Dim futurePricesGroup As New ColumnHeader(True) Dim restGroup As New ColumnHeader(True)
customerGroup.Caption = "Customer" customerGroup.Style.HorizontalAlign = HorizontalAlign.Center customerGroup.RowLayoutColumnInfo.OriginX = 0 customerGroup.RowLayoutColumnInfo.OriginY = 0
chargeParametersGroup.Caption = "Charge Parameters" chargeParametersGroup.Style.HorizontalAlign = HorizontalAlign.Center chargeParametersGroup.RowLayoutColumnInfo.OriginX = 2 chargeParametersGroup.RowLayoutColumnInfo.OriginY = 0
currentPricesGroup.Caption = "Current Prices" currentPricesGroup.Style.HorizontalAlign = HorizontalAlign.Center currentPricesGroup.Style.BackColor = Drawing.Color.FromArgb(204, 255, 204) currentPricesGroup.RowLayoutColumnInfo.OriginX = 4 currentPricesGroup.RowLayoutColumnInfo.OriginY = 0
futurePricesGroup.Caption = "Future Prices" futurePricesGroup.Style.HorizontalAlign = HorizontalAlign.Center futurePricesGroup.Style.BackColor = Drawing.Color.FromArgb(255, 255, 153) futurePricesGroup.RowLayoutColumnInfo.OriginX = 8 futurePricesGroup.RowLayoutColumnInfo.OriginY = 0
restGroup.Caption = " " restGroup.RowLayoutColumnInfo.OriginX = 12 restGroup.RowLayoutColumnInfo.OriginY = 0
' Add the newly created Header object to the HeaderLayout object e.Layout.Bands(0).HeaderLayout.Add(customerGroup) e.Layout.Bands(0).HeaderLayout.Add(chargeParametersGroup) e.Layout.Bands(0).HeaderLayout.Add(currentPricesGroup) e.Layout.Bands(0).HeaderLayout.Add(futurePricesGroup) e.Layout.Bands(0).HeaderLayout.Add(restGroup)
e.Layout.Bands(0).HeaderStyle.Height = Unit.Point(15) e.Layout.Bands(0).HeaderStyle.Font.Name = "MS Sans Serif" e.Layout.Bands(0).HeaderStyle.Font.Bold = True e.Layout.Bands(0).HeaderStyle.Font.Size = FontSize.Large
customerGroup.RowLayoutColumnInfo.SpanX = 2 chargeParametersGroup.RowLayoutColumnInfo.SpanX = 2 currentPricesGroup.RowLayoutColumnInfo.SpanX = 4 futurePricesGroup.RowLayoutColumnInfo.SpanX = 4
Protected Sub UltraWebGrid1_InitializeRow(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.RowEventArgs) Handles UltraWebGrid1.InitializeRow Try Dim isBinding As Boolean = CType(Session.Item("isBinding"), Boolean) If (isBinding) Then 'e.Row.Cells("Select").Value = False
For Each cl As UltraGridCell In e.Row.Cells If (cl.Column.Header.Caption = "Select") Then cl.AllowEditing = AllowEditing.Yes End If If (Not (cl.Column.Header.Caption = "Select")) Then cl.AllowEditing = AllowEditing.No End If Next End If Catch ex As Exception 'showMessage(ex.Message + " ** in function UltraGrid1_InitializeRow", "") End Try End SubEnd Class
Public Class GridHeaderTemplate Implements ITemplate
Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn ' Cast the container to a HeaderItem Dim headerItem As HeaderItem = DirectCast(container, HeaderItem)
Dim checkBox As New CheckBox()
Dim cb As New CheckBox() cb.ID = "headerCB" 'cb.Text = "Select" cb.Attributes.Add("onclick", "HeaderCheckedChanged();") headerItem.Controls.Add(cb) End SubEnd Class
Public Class PlaceHolderTemplate Implements ITemplate Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn ' Cast the container to a CellItem Dim cellitem As CellItem = DirectCast(container, CellItem) Dim cb As New CheckBox() cb.ID = "cellCB" cellitem.Controls.Add(cb) End SubEnd Class
Thanks
Saran
Excellent point, I should have done that in the first place. The setup needs to be completed in the OnInit as Page_Load happens too late in the page lifecycle. I've updated the code to reflect this change.
Hello Saran,
Can you move the code that dynamically sets up the templated column from Page_Load to OnInit and see if this works better for you ?