I had a need for a combobox that allowed multiselect and checkbox. WebCombo was very close to what I needed, with some simple code i was able to achieve this functionality so i figured i would share with others who may be interested.
Steps:
Drop a webcombo on your form:
In page_load add the following code: ( I set properties in code for webcombo)
{
//create datasource
tbl.Columns.Add("Id", typeof(int));
tbl.Columns.Add("Text");
}
WebCombo1.DataSource = tbl;
WebCombo1.DataBind();
//just some adjusting of the height
//get a reference to the grid in the combo
//set some properties of the grid to act and look like a combo
grid.DisplayLayout.ClientSideEvents.CellClickHandler = "GridCellClickHandler";
grid.Columns.FromKey("Checked").CellStyle.BorderWidth = Unit.Pixel(0);
grid.DisplayLayout.ActivationObject.BorderStyle = BorderStyle.None;
grid.Columns.FromKey("Text").Width = Unit.Pixel(160);
//set clientside events Combo box Properties for look
WebCombo1.ClientSideEvents.BeforeCloseUp = "WebCombo1_BeforeCloseUp";
WebCombo1.DropDownLayout.RowSelectors = RowSelectors.No;
WebCombo1.DropDownLayout.ColHeadersVisible = ShowMarginInfo.No;
WebCombo1.DropDownLayout.FrameStyle.BackColor = System.Drawing.Color.Transparent;
//if set to false then selected values won't show in combo until you click another spot on the form
Then add the following javascript section to handle clientside events:
var cancelClose=false;
//prevents closing of combo while selecting multiple items
//Cancels premature closing.
//Loop through grid and find all rows that are checked and concatenate the text.
var grid=combo.getGrid();
var displayText='';
var checked=row.getCell(1).getValue();
//display the text in the combo text box
combo.setDisplayValue(displayText);
//Changes checkbox selection on any cell click in the row
boolCell.setValue(!boolCell.getValue());
cancelClose=true;
//Prevents dropdown from cloasing after clicking checkbox
That should do it.
Some slight modifications will need to be made for you coumn names/numbers
To retrieve values on client, use a functioin such as:
var selectedText=new Array();
var row=grid.Rows.getRow(i);
selectedValues[aCnt]=row.getCell(0).getValue();
selectedText[aCnt]=row.getCell(2).getValue();
aCnt++;
To get selected values on the server, the following would do it:
bool isChecked = (bool)row.Cells.FromKey("Checked").Value;
//listbox is a ListBox server control
I also attached the project to this post.
Here is what it looks like:
WebCombo1_BeforeSelectChange(webComboId) {
cancelClose =
true; // prevent close up on very first click.
;
Now I have tried this code but I can't seem to get the DisplayLayout.ClientSideEvents.CellClickHandler = "GridCellClickHandler" to fire.
Any suggestions? I'm using version 8.2.20082.2110
If Not IsPostBack Then
'get a reference to the grid in the combo
With grid.DisplayLayout.ClientSideEvents
.CellClickHandler = "GridCellClickHandler"
.AfterCellUpdateHandler = "GridAfterCellUpdateHandler"
End With
End If
The same way can we do with wincombo?
Thanks for sharing
I found that by tweaking the GridCellClickHandler I got this bug fixed:
function GridCellClickHandler(gridName, cellId, button){ //Changes checkbox selection on any cell click in the row cancelClose=true; var cell=igtbl_getCellById(cellId); var boolCell=cell.Row.getCell(1); if(cell.Index!=1) boolCell.setValue(!boolCell.getValue());}