下拉列表中的requiredfieldvalidator [英] Requiredfieldvalidator in Dropdown

查看:46
本文介绍了下拉列表中的requiredfieldvalidator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个DropDownList和RequiredFieldValidator,用于检查是否
用户已为DropDownList选择值.

我在运行时填充下拉列表,并添加了两个项目(请选择"和全部").

请选择"选项的值为".

我面临的问题是,当我选择请选择"时
选项显示RequiredFieldValidator的错误消息
大约一秒钟,然后页面回发.

我希望显示错误消息,并且该页面不应该回发.

我在这里做错什么了吗?

我可以在服务器端检查所选项目的值,但这只是一次往返的浪费. DropDownList的AutoPostBack属性设置为true.

任何帮助,将不胜感激 .

在此先谢谢您.

Hi,
I have a DropDownList and a RequiredFieldValidator that checks if a
value has been selected for the DropDownList by the user.

I am populating the drop down at runtime and adding two items, ( "Please select" and "All").

The "Please select" option has a value of "".

The problem I am facing is that when I select the "Please select"
option the error message of the RequiredFieldValidator gets displayed
for a second or so and then the page posts back.

I wanted that the error message should be displayed and the page should not post back.

Am I doing something wrong here?

I could check the value of the selected item at the server side but that is just a waste of one round trip. The AutoPostBack property of the DropDownList is set to true.

Any help would be appreciated .

Thanks in advance.

推荐答案

您可以使用比较验证器.
示例:

You can use compare validator.
Example:

<asp:CompareValidator

                          id="cvNumberOfGuests"

                          runat="server"

                          ControlToValidate="ddlNumberOfGuests"

                          ErrorMessage="You must select the number of guests (1-5)."

                          ValueToCompare="0"

                          display="Dynamic"

                          Type="Integer"

                          Operator="GreaterThan">*</asp:CompareValidator>
                <asp:DropDownList ID="ddlNumberOfGuests" Width="100%" runat="server">
                    <asp:ListItem Value="0" Selected="true">Number of guests</asp:ListItem>
                    <asp:ListItem Value="1">1</asp:ListItem>
                    <asp:ListItem Value="2">2</asp:ListItem>
                    <asp:ListItem Value="3">3</asp:ListItem>
                    <asp:ListItem Value="4">4</asp:ListItem>
                    <asp:ListItem Value="5">5</asp:ListItem>
                    <asp:ListItem Value="6">Contact us for more than 5 guest</asp:ListItem>
                </asp:DropDownList>


假设您的下拉列表中包含此项目..

suppose you are having this items in your dropdownlist..

DropDownList1.Items.Add(new ListItem("--Select--","0"));
DropDownList1.Items.Add(new ListItem("Hiren","1"));
DropDownList1.Items.Add(new ListItem("Mohini","2"));
DropDownList1.Items.Add(new ListItem("Chris", "3"));



然后将requiredfieldvalidator''s controltovalidate 属性设置为"DropDownlist1",并将requiredfieldvalidator 的"InitialValue"属性设置为"0",因为这将是"--Select--"的值您可以在上方显示..

希望对您有帮助.
如果您还有其他疑问,请告诉我们..



then.. set the requiredfieldvalidator''s controltovalidate property to "DropDownlist1" and set the "InitialValue" property of the requiredfieldvalidator to "0" as this will be the value for "--Select--" you can show above..

hope it helps.
please let us know if you are having some another doubt than this one..


这篇关于下拉列表中的requiredfieldvalidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆