C# 检查框列表的编程创建的CustomValidator ServerValidate事件有问题
我正在尝试使用C为asp.net CheckBoxList动态创建CustomValidator。我似乎很好地完成了验证程序的创建,但是ServerValidate给我带来了一些麻烦。我根据我手动创建的一个CustomValidator对其进行建模,该程序运行良好,因此我不太确定需要做哪些不同的工作 这里是手动CustomValidator,它可以完美地工作。这是我的模型:C# 检查框列表的编程创建的CustomValidator ServerValidate事件有问题,c#,asp.net,C#,Asp.net,我正在尝试使用C为asp.net CheckBoxList动态创建CustomValidator。我似乎很好地完成了验证程序的创建,但是ServerValidate给我带来了一些麻烦。我根据我手动创建的一个CustomValidator对其进行建模,该程序运行良好,因此我不太确定需要做哪些不同的工作 这里是手动CustomValidator,它可以完美地工作。这是我的模型: <asp:CustomValidator ID="CustomV_JobPositions" runat="serv
<asp:CustomValidator ID="CustomV_JobPositions" runat="server"
ErrorMessage="Please select at least one job position."
OnServerValidate="CustomV_JobPositions_ServerValidate"
ValidationGroup="JobPositions" Display="Dynamic" ForeColor="Red">
</asp:CustomValidator>
<asp:CheckBoxList ID="CBL_JobPositions" runat="server"
DataSourceID="JobPositionsEntity" DataTextField="JobPosition1"
DataValueField="JobPositionID" RepeatColumns="3" Width="100%"
OnSelectedIndexChanged="CBL_JobPositions_SelectedIndexChanged"
AutoPostBack="True" ValidationGroup="JobPositions">
</asp:CheckBoxList>
因此,以上所有操作都很好。
然后我尝试以动态/编程方式复制它:
public CustomValidator CreateDynamicCustomCheckBoxListValidator(string ControlToValidate, string ValidationGroup)
{
CustomValidator CV = new CustomValidator(); // Set up a new CustomValidator
CV.ID = "CustomV_" + ControlToValidate; // Set Validator ID with the name of desired control
CV.ErrorMessage = "At Least One Selection Required"; // ... then set the error message to "at least one selection required"
CV.ForeColor = System.Drawing.Color.Red; // Make the validator error display red
CV.ValidationGroup = ValidationGroup; // Assign this validator to a group
CV.Display = ValidatorDisplay.Dynamic; // Make the validator display dynamically
CV.ServerValidate += new ServerValidateEventHandler(CustomV_CheckBoxList_ServerValidate);
return CV; // Return the completed validator that was just created
}
private void CustomV_CheckBoxList_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = false; // Assume initially that nothing is checked
CheckBoxList CBL = (CheckBoxList)source;
foreach (ListItem item in CBL.Items) // Loop through each list item in the checkbox
{
if (item.Selected == true) // If at least one item is checked...
{ args.IsValid = true; } // ... then mark the whole list as valid...
}
}
它似乎毫不费力地创建了CustomValidator,但在实际执行验证时,它会抛出以下错误:
无法将“System.Web.UI.WebControl.CustomValidator”类型的对象强制转换为“System.Web.UI.WebControl.CheckBoxList”类型
唯一的问题是,我需要循环检查复选框列表,如果没有该转换,我无法对其进行编程,以便对源代码执行任何操作。例如,下面的代码给了我这个错误:foreach语句无法对“object”类型的变量进行操作,因为“object”不包含“GetEnumerator”的公共定义
private void CustomV_CheckBoxList_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = false; // Assume initially that nothing is checked
//CheckBoxList CBL = (CheckBoxList)source;
foreach (ListItem item in source) // Loop through each list item in the checkbox
{
if (item.Selected == true) // If at least one item is checked...
{ args.IsValid = true; } // ... then mark the whole list as valid...
}
}
有没有关于如何让它工作的想法/我做错了什么?这是一篇老文章,但我遇到了一个类似的问题,所以我想我会发布我是如何绕过这个问题的 创建从CustomValidator继承的对象,并为其提供名为ControlId的属性: 公共类CustomValidatorWithValidatedControlAttributes:CustomValidator { 公共字符串ControlId{get;set;} } 动态创建CustomValidatorWithValidatedControlAttributes的实例,提供ControlId属性: var customValidator=新的CustomValidatorWithValidatedControlAttributes { ID=string.ConcatTheControl.ID,_customFieldValidator, EnableClientScript=false, Display=ValidatorDisplay.Dynamic, ErrorMessage=必需, CssClass=错误, ControlId=control.ID }; customValidator.ServerValidate+=customValidator\u ServerValidate 然后在ServerValidate方法中将对象强制转换回CustomValidatorWithValidatedControlAttributes类,并从ControlId属性访问复选框,然后检查是否选中该复选框:
void customValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
var customValidator = source as CustomValidatorWithValidatedControlAttributes;
if (customValidator != null)
{
var checkBox = FindControl(customValidator.ControlId) as CheckBox;
if (checkBox != null)
{
args.IsValid = checkBox.Checked;
}
}
args.IsValid = false;
}
void customValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
var customValidator = source as CustomValidatorWithValidatedControlAttributes;
if (customValidator != null)
{
var checkBox = FindControl(customValidator.ControlId) as CheckBox;
if (checkBox != null)
{
args.IsValid = checkBox.Checked;
}
}
args.IsValid = false;
}