 |
|
 |
I like the simplicity of it. I will definitely find uses for this. Thank you.
|
|
|
|
 |
|
 |
When i tried to register the control it fails cause it couldn't find the assembly file.
|
|
|
|
 |
|
 |
If you save "The Code" into a .cs file and compile it then it should work fine.
If you're still having trouble, provide a little more information, such as the steps you've taken, and what the error message says exactly.... And I'll see if I can help.
Cheers.
|
|
|
|
 |
|
 |
When i tried to register the control it fails cause it couldn't find the assembly file.
|
|
|
|
 |
|
|
 |
|
 |
I'm not sure, if you try it out let us know
|
|
|
|
 |
|
 |
Hi, I'm developing a property page to be extended in Active Directory. I'm using VS 2005 VC++ and the property page is created in a in-process COM project with a dialog user interface. Can I use CheckBoxList in this? Or is it only for Winform and ASP development? Because I can't see it in the toolbox.
Thanks,
Alpha
|
|
|
|
 |
|
 |
I don't know how this relates to my validator?
However I don't know the answer to your question, good luck with that.
|
|
|
|
 |
|
 |
This works well, you just need to change this:
var col = target.all;
to this:
var col = target;
That got it working for me.
|
|
|
|
 |
|
 |
OK, that code breaks the CheckBoxList control. Here's a better fix:
function ListItemVerify(val)
{
var target = document.all[val.controltovalidate];
var col = target;
if (col.length == 0 || col.length == null)
{
col = target.all;
}
etc...
|
|
|
|
 |
|
 |
Sweet, thanks for that, I'll have to update the article.
|
|
|
|
 |
|
 |
http://buy-medications-online.ceroline.info/
http://buy-medications-online.ceroline.info/
|
|
|
|
 |
|
 |
There's still a problem with this code: it only works in IE. Why not use a method that returns the item no matter what browser you are using:
var target;
if (document.all)
{
target = document.all[name];
}
else if (document.getElementById)
{
target = document.getElementById(name);
}
if (target)
{
.
.
.
|
|
|
|
 |
|
 |
Yes, but this alone won't get it working in firefox.
If you have .net 2.0 also include the following to get it working.... I think I'll have to try it sometime
replace this line:
this.Attributes["evaluationfunction"] = "ListItemVerify";
with this line:
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "ListItemVerify");
Does .net 2.0 have a CheckBoxList validator out of the box?
|
|
|
|
 |
|
 |
I tried your suggestion (and read the same suggestion elsewhere) but still isn't working in FF2.
|
|
|
|
 |
|
 |
Hi Jcollum
I've just submitted an update version which works in FF2
watch this space
Cheers.
Murray.
|
|
|
|
 |
|
 |
I got it hitting the js by adding:
this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "ListItemVerify");
this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "clientvalidationfunction ", "ListItemVerify");
but it still doesn't validate properly cause the js is a bit outdated. Hope the new version fixes this, this control is very helpful.
|
|
|
|
 |
|
 |
HOw long does it take them to get the article updated? Must be some sort of moderated process...
|
|
|
|
 |
|
 |
New version works. Thanks Murray!
|
|
|
|
 |
|
 |
Here's another way to go... my way
This script allows the validation of DropDownList controls.
function ListItemVerify(val) {
if (typeof(val.controltovalidate) == "string") {
var selectedCount = 0, control = document.getElementById(val.controltovalidate);
if (control.tagName.toLowerCase() == "select") {
selectedCount = new Number(control.selectedIndex > -1);
}
else for (i = 0;; i++) {
if ((currentListItem = document.getElementById(control.id + '_' + i.toString())) == null) break;
selectedCount += new Number(currentListItem.checked);
}
return selectedCount >= parseInt(val.minimumNumberOfSelectedCheckBoxes);
}
return true;
}
Hope this proofs useful.
Miguel Hasse
|
|
|
|
 |
|
 |
Hi Miguel, You can also use a plain old <asp:RequiredFieldValidator ... /> for DropDownList Control validation. Simply ensure the first (default) item has a blank value: <asp:ListItem value="">Please Choose One</asp:ListItem> If you're databinding, bind the list, then insert the 'please choose one' list item at position 0 programatically.
|
|
|
|
 |
|
 |
Is there a way to modify the JS and server-side code to validate against the user checking over X amount
|
|
|
|
 |
|
 |
Sure, should be pretty easy, off the top of my head, update this method like so: /// <summary> /// Return true if an item in the list is selected. /// </summary> /// <returns>true if ControlToValidate /// has one item or more selected</returns> protected bool EvaluateIsChecked() { ListControl listToValidate = ((ListControl) this.FindControl(this.ControlToValidate)); int selectedCount =0; foreach( ListItem li in listToValidate.Items ) { if ( li.Selected == true ) selectedCount++; } return (selectedCount>=MinimumToSelect); } and add this: private int _minimumToSelect = 1; public int MinimumToSelect { get{return _minimumToSelect;} set{_minimumToSelect = value;} } AND do something similar in the javascript, I'll leave you to work that one out. Cheers. Murray.
|
|
|
|
 |
|
 |
[ToolboxData("<{0}:ListControlRequiredFieldValidator runat="server">{0}:ListControlRequiredFieldValidator>")]
[DefaultProperty("ErrorMessage")] // make the error message the default
public class ListControlRequiredFieldValidator : BaseValidator
{
private int _minimumToSelect = 0;
public int MinimumToSelect
{
get { return _minimumToSelect; }
set
{
_minimumToSelect = value;
}
}
private int _maximumToSelect = 0;
public int MaximumToSelect
{
get { return _maximumToSelect; }
set
{
_maximumToSelect = value;
}
}
/// True if dependencies are valid.
protected override bool ControlPropertiesValid()
{
Control controlToValidate =
FindControl(ControlToValidate) as ListControl;
return (controlToValidate != null);
}
///
/// Validator Requirement
///
/// true if ControlToValidate
/// has one item or more selected
protected override bool EvaluateIsValid()
{
return this.EvaluateIsChecked();
}
///
/// Return true if an item in the list is selected.
///
/// true if ControlToValidate
/// has one item or more selected
protected bool EvaluateIsChecked()
{
ListControl listToValidate =
((ListControl)this.FindControl(this.ControlToValidate));
int selectedCount = 0;
foreach (ListItem li in listToValidate.Items)
{
if (li.Selected == true)
selectedCount++;
}
if (selectedCount < MinimumToSelect && MinimumToSelect > 0)
return false;
if (selectedCount > MaximumToSelect && MaximumToSelect > 0)
return false;
return true;
}
///
/// Pre Render
///
///
protected override void OnPreRender(EventArgs e)
{
if (_minimumToSelect > 0 && _maximumToSelect > 0)
ErrorMessage = string.Format("Minimum {0}, Maximum {1} elements can be selected...", MinimumToSelect, MaximumToSelect);
else if (_minimumToSelect == 0 && _maximumToSelect > 0)
ErrorMessage = string.Format("Maximum {0} elements can be selected...", MaximumToSelect);
else if (_minimumToSelect > 0 && _maximumToSelect == 0)
ErrorMessage = string.Format("Minimum {0} element(s) must be selected", MinimumToSelect);
System.Web.HttpContext.Current.Trace.Write(
"Override OnPreRender");
if (this.DetermineRenderUplevel() && this.EnableClientScript)
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID,
"evaluationfunction", "ListItemVerify");
Page.ClientScript.RegisterExpandoAttribute(this.ClientID,
"minimumNumberOfSelectedCheckBoxes", MinimumToSelect.ToString());
Page.ClientScript.RegisterExpandoAttribute(this.ClientID,
"maximumNumberOfSelectedCheckBoxes", MaximumToSelect.ToString());
//TODO: imporove to allow variable number.
this.RegisterClientScript();
}
else
{
this.Attributes.Remove("evaluationfunction");
}
base.OnPreRender(e);
}
///
/// Register the client script.
///
protected void RegisterClientScript()
{
string script = @"
<script language=""javascript"">
function ListItemVerify(val)
{
var control =
document.getElementById(val.controltovalidate);
var minimumNumberOfSelectedCheckBoxes =
parseInt(val.minimumNumberOfSelectedCheckBoxes);
var maximumNumberOfSelectedCheckBoxes =
parseInt(val.maximumNumberOfSelectedCheckBoxes);
var selectedItemCount = 0;
var liIndex = 0;
var currentListItem =
document.getElementById(control.id +
'_' + liIndex.toString());
while (currentListItem != null)
{
if (currentListItem.checked) selectedItemCount++;
liIndex++;
currentListItem =
document.getElementById(control.id +
'_' + liIndex.toString());
}
if (selectedItemCount < minimumNumberOfSelectedCheckBoxes && minimumNumberOfSelectedCheckBoxes > 0)
return false;
if (selectedItemCount > maximumNumberOfSelectedCheckBoxes && maximumNumberOfSelectedCheckBoxes > 0)
return false;
return true;
}
</script>
";
this.Page.ClientScript.RegisterClientScriptBlock(
typeof(ListControlRequiredFieldValidator),
"ListRequiredValidator_Script", script);
}
}
|
|
|
|
 |
|
 |
By the way thank to Murray Roke for this nice code
|
|
|
|
 |