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:
Thanks for sharing!
If only this have been posted earlier it would have saved me a lot of time :) I just posted about my own multiselect combo a while ago to help out on another post here in the forums only to find out about this post which came days earlier.
Anyways, I'd still definitely get a lot from this.
I seem to ran into an odd behavior though if clicking directly on the checkbox. For example, right after the page loads and I dropdown the combo and click on a checkbox, it seems to close up right away instead of the normal behavior of allowing another selection. And also, same but more pronounced issue when using firefox. Any chance you could look into it too.
I used an unbound Checkbox column (instead of a boolean datacolumn) but firefox doesn't seem to persist when page is posted back to the server so had to use some work around so if you could look into the one above it would be great. As the stuff i coded to keep the dropdown from closing I would have to say could use some improvement.
I'm running into the same problem, where if you click directly onto the checkbox the dropdownlist closes up after clicking on the first item. I tried hooking the GridBeforeCellUpdateHandler to set cancelClose to true, but this did not help. Any idea what event I need to hook in order to catch this?
WebCombo1_BeforeSelectChange(webComboId) {
cancelClose =
true; // prevent close up on very first click.
;