I'm working with ASP.NET 3.5. I have a list box that users must add items to (I've written the code for this). My requirement is that at least one item must be added to the listbox or they cannot submit the form. I have several other validators on the page and they all write to a ValidationSummary control. I would like this listbox validation to write to the Validation Summary control as well. Any help is greatly appreciated. Thank you.
我正在使用ASP.NET 3.5。我有一个用户必须添加项目的列表框(我已经为此编写了代码)。我的要求是必须至少将一个项目添加到列表框中,否则他们无法提交表单。我在页面上有几个其他验证器,它们都写入ValidationSummary控件。我希望此列表框验证也写入验证摘要控件。任何帮助是极大的赞赏。谢谢。
7 个解决方案
#1
5
Drop in a custom validator, Add your desired error message to it, double click on the custom validator to get to the code behind for the event handler, and then you would implement server side like this:
放入自定义验证器,向其中添加所需的错误消息,双击自定义验证器以获取事件处理程序的代码,然后您将实现服务器端,如下所示:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = ListBox1.Items.Count > 0;
}
Also you can implement client side javascript as well.
您也可以实现客户端javascript。
I just threw this up on a page and tested it quickly, so you might need to tweak it a bit: (The button1 only adds an item to the List Box)
我只是把它扔到一个页面上并快速测试,所以你可能需要稍微调整一下:( button1只在列表框中添加一个项目)
<script language="JavaScript">
<!--
function ListBoxValid(sender, args)
{
args.IsValid = sender.options.length > 0;
}
// -->
</script>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" ValidationGroup="NOVALID" />
<asp:Button ID="Button2" runat="server" Text="ButtonsUBMIT" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator"
onservervalidate="CustomValidator1_ServerValidate" ClientValidationFunction="ListBoxValid"></asp:CustomValidator>
If you add a validation summary to the page, you error text should show up in that summary if there is no items in the ListBox, or other collection-able control, what ever you want to use, as long as the ValidationGroup is the same.
如果向页面添加验证摘要,只要ValidationGroup相同,如果ListBox或其他可收集控件中没有项目,只要ValidationGroup相同,那么错误文本应显示在该摘要中。
#2
5
This didn't work for me:
这不适合我:
function ListBoxValid(sender, args)
{
args.IsValid = sender.options.length > 0;
}
But this did:
但这样做:
function ListBoxValid(sender, args)
{
var ctlDropDown = document.getElementById(sender.controltovalidate);
args.IsValid = ctlDropDown.options.length > 0;
}
#3
3
gotta make sure to add these properties to the CustomValidator:
必须确保将这些属性添加到CustomValidator:
Display="Dynamic" ValidateEmptyText="True"
#4
1
<asp:CustomValidator
runat="server"
ControlToValidate="listbox1"
ErrorMessage="Add some items yo!"
ClientValidationFunction="checkListBox"
/>
<script type="Text/JavaScript">
function checkListBox(sender, args)
{
args.IsValid = sender.options.length > 0;
}
</script>
#5
1
Actually this is the proper way to make this work (as far as the JavaScript is concerned).
实际上,这是实现这项工作的正确方法(就JavaScript而言)。
ListBox.options.length will always be your total number of options, not the number you have selected. The only way I have found that works is to use a for loop to go through the list.
ListBox.options.length将始终是您的选项总数,而不是您选择的数字。我找到的唯一方法是使用for循环遍历列表。
function ListBoxValid(sender, args)
{
var listBox = document.getElementById(sender.controltovalidate);
var listBoxCnt = 0;
for (var x =0; x<listBox.options.length; x++)
{
if (listBox.options[x].selected) listBoxCnt++;
}
args.IsValid = (listBoxCnt>0)
}
#6
0
this work for me
这项工作对我而言
<script language="JavaScript">
function CheckListBox(sender, args)
{
args.IsValid = document.getElementById("<%=ListBox1.ClientID%>").options.length > 0;
}
</script>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="*Required" ClientValidationFunction="CheckListBox"></asp:CustomValidator>
#7
-1
You will want to register your control with the page by sending in the ClientID. Then, you can use Microsoft AJAX to grab your control and check the values.
您需要通过发送ClientID向页面注册您的控件。然后,您可以使用Microsoft AJAX来获取控件并检查值。
#1
5
Drop in a custom validator, Add your desired error message to it, double click on the custom validator to get to the code behind for the event handler, and then you would implement server side like this:
放入自定义验证器,向其中添加所需的错误消息,双击自定义验证器以获取事件处理程序的代码,然后您将实现服务器端,如下所示:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = ListBox1.Items.Count > 0;
}
Also you can implement client side javascript as well.
您也可以实现客户端javascript。
I just threw this up on a page and tested it quickly, so you might need to tweak it a bit: (The button1 only adds an item to the List Box)
我只是把它扔到一个页面上并快速测试,所以你可能需要稍微调整一下:( button1只在列表框中添加一个项目)
<script language="JavaScript">
<!--
function ListBoxValid(sender, args)
{
args.IsValid = sender.options.length > 0;
}
// -->
</script>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" ValidationGroup="NOVALID" />
<asp:Button ID="Button2" runat="server" Text="ButtonsUBMIT" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator"
onservervalidate="CustomValidator1_ServerValidate" ClientValidationFunction="ListBoxValid"></asp:CustomValidator>
If you add a validation summary to the page, you error text should show up in that summary if there is no items in the ListBox, or other collection-able control, what ever you want to use, as long as the ValidationGroup is the same.
如果向页面添加验证摘要,只要ValidationGroup相同,如果ListBox或其他可收集控件中没有项目,只要ValidationGroup相同,那么错误文本应显示在该摘要中。
#2
5
This didn't work for me:
这不适合我:
function ListBoxValid(sender, args)
{
args.IsValid = sender.options.length > 0;
}
But this did:
但这样做:
function ListBoxValid(sender, args)
{
var ctlDropDown = document.getElementById(sender.controltovalidate);
args.IsValid = ctlDropDown.options.length > 0;
}
#3
3
gotta make sure to add these properties to the CustomValidator:
必须确保将这些属性添加到CustomValidator:
Display="Dynamic" ValidateEmptyText="True"
#4
1
<asp:CustomValidator
runat="server"
ControlToValidate="listbox1"
ErrorMessage="Add some items yo!"
ClientValidationFunction="checkListBox"
/>
<script type="Text/JavaScript">
function checkListBox(sender, args)
{
args.IsValid = sender.options.length > 0;
}
</script>
#5
1
Actually this is the proper way to make this work (as far as the JavaScript is concerned).
实际上,这是实现这项工作的正确方法(就JavaScript而言)。
ListBox.options.length will always be your total number of options, not the number you have selected. The only way I have found that works is to use a for loop to go through the list.
ListBox.options.length将始终是您的选项总数,而不是您选择的数字。我找到的唯一方法是使用for循环遍历列表。
function ListBoxValid(sender, args)
{
var listBox = document.getElementById(sender.controltovalidate);
var listBoxCnt = 0;
for (var x =0; x<listBox.options.length; x++)
{
if (listBox.options[x].selected) listBoxCnt++;
}
args.IsValid = (listBoxCnt>0)
}
#6
0
this work for me
这项工作对我而言
<script language="JavaScript">
function CheckListBox(sender, args)
{
args.IsValid = document.getElementById("<%=ListBox1.ClientID%>").options.length > 0;
}
</script>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="*Required" ClientValidationFunction="CheckListBox"></asp:CustomValidator>
#7
-1
You will want to register your control with the page by sending in the ClientID. Then, you can use Microsoft AJAX to grab your control and check the values.
您需要通过发送ClientID向页面注册您的控件。然后,您可以使用Microsoft AJAX来获取控件并检查值。