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
Hello,
Alright so to do this we need to add the checkbox to the header template before the column is even created... so before I assigned the datasource property of the grid I needed to setup the template column. <In the snippet you added I am guessing you did that in the InitializeLayout event handler or after you've set the DataSource and called DataBind and that is too late to hook items into the HeaderTemplate, do it in the Page's OnInit instead.>
So here's the code to do that column setup:
C#
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// Create a templated column
TemplatedColumn col = new TemplatedColumn(true);
this.UltraWebGrid1.DisplayLayout.Bands[0].Columns.Add(col);
col.Key = "CheckBoxCol";
col.Header.Caption = "";
GridHeaderTemplate tempHeader = new GridHeaderTemplate();
// Set the header template.
col.HeaderTemplate = tempHeader;
this.UltraWebGrid1.DataSource = LoadGrid();
this.UltraWebGrid1.DataBind();
}
VB.NET
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
' Create a templated column Dim col As New TemplatedColumn(True) Me.UltraWebGrid1.DisplayLayout.Bands(0).Columns.Add(col) col.Key = "CheckBoxCol" col.Header.Caption = "" Dim tempHeader As New GridHeaderTemplate() ' Set the header template. col.HeaderTemplate = tempHeader Me.UltraWebGrid1.DataSource = LoadGrid() Me.UltraWebGrid1.DataBind() End Sub
So now what does my GridHeaderTemplate look like:
public class GridHeaderTemplate : ITemplate
public void InstantiateIn(Control container)
// Cast the container to a HeaderItem
HeaderItem headerItem = (HeaderItem)container;
CheckBox checkBox = new CheckBox();
CheckBox cb = new CheckBox();
cb.ID = "headerCB";
cb.Attributes.Add("onclick", "HeaderCheckedChanged();");
headerItem.Controls.Add(cb);
Public Class GridHeaderTemplate Implements ITemplate Public Sub InstantiateIn(ByVal container As Control) ' 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.Attributes.Add("onclick", "HeaderCheckedChanged();") headerItem.Controls.Add(cb) End Sub End Class
Next I setup the Cell Template dynamically in the WebGrid's InitializeLayout event handler:
TemplatedColumn col = (TemplatedColumn)(e.Layout.Bands[0].Columns.FromKey("CheckBoxCol"));
PlaceHolderTemplate tempCell = 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);
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "CheckBoxKey", "<script type='text/javascript'>var headerCheckBoxID = '" + col.HeaderItem.FindControl("headerCB").ClientID + "';</script>");
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "GridKey", "var gridID = '" + this.UltraWebGrid1.ClientID + "';", true);
Dim col As TemplatedColumn = DirectCast((e.Layout.Bands(0).Columns.FromKey("CheckBoxCol")), 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)
The PlaceHolderTemplate used for the Cell Template should be awfully familiar now that we've seen the header...
public class PlaceHolderTemplate : ITemplate
// Cast the container to a CellItem
CellItem cellitem = (CellItem)container;
cb.ID = "cellCB";
cellitem.Controls.Add(cb);
Public Class PlaceHolderTemplate Implements ITemplate Public Sub InstantiateIn(ByVal container As Control) ' 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 Sub End Class
Now I told the Header Checkbox to call a javascript function when its onclick fires... so here's the js I used to toggle the checkbox:
<script type="text/javascript">
function HeaderCheckedChanged(){
var headerCheckBox = document.getElementById(headerCheckBoxID);
var grid = igtbl_getGridById(gridID);
for (i = 0; i < grid.Rows.length; i++)
grid.Rows.getRow(i).getCellFromKey("CheckBoxCol").getElement().children[0].checked = headerCheckBox.checked;
</script>
The javascript function can be written in several ways and accomplish the same task this is merely one way to accomplish this task.
Also as you can see in the code behind I registered a script block to render the grid's id to the client for use in my HeaderCheckedChanged function and I also threw the Header CheckBox's Id out to the client as well. You can use a stringbuilder and do this part a little neater but I think the point of what I've done to solve the task presented has been displayed in these code snippets.
What have others done in the past to solve this task?
EDIT: Updated code snippets to handle OnInit instead of Page_Load as page load was again too late in the Page's Lifecycle.
Mathew
When binding the second time.. one thing which I noticed is "InstantiateIn" of GridHeaderTemplate doesn't fire, because of which the checkbox is not getting added. Another thing is the reference to "col.HeaderItem.FindControl("headerCB").ClientID" is not available which again because the HeaderCB didn't get added. Any help?
Thanks
Saran
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 ?
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)
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
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.