使用DropDownList的ASP.Net自定义验证器控件
以下代码用于使用Custom Validator验证DropDownList控件.
Default1.aspx <td> <asp:DropDownList ID="DDL_Product" runat="server" Height="21px" Width="128px"> <asp:ListItem>Select</asp:ListItem> <asp:ListItem>Nokia</asp:ListItem> <asp:ListItem>LG</asp:ListItem> <asp:ListItem>Samsung</asp:ListItem> <asp:ListItem>sony</asp:ListItem> <asp:ListItem>Micromax</asp:ListItem> <asp:ListItem>Karbonn</asp:ListItem> <asp:ListItem>Apple</asp:ListItem> </asp:DropDownList> </td> <td> <asp:CustomValidator ID="cv1" Display="Dynamic" ControlToValidate = "DDL_Product" OnServerValidate="ddl_server" runat="server" ForeColor="Red" ErrorMessage="Please Select the Product"></asp:CustomValidator> </td> Default1.aspx.cs protected void ddl_server(object sender,ServerValidateEventArgs e) { if (e.Value.selectedIndex <= 0) { e.IsValid = true; } else { e.IsValid = false; } } 以上验证未经验证. 解决方法
你应该使用RequireValidator.
1)添加“选择”项的值,将用于验证初始值: <asp:DropDownList ID="DDL_Product" runat="server" Height="21px" Width="128px"> <asp:ListItem Value="0">Select</asp:ListItem> /*Rest of items*/ </asp:DropDownList> 2)然后像这样使用RequireValidator,比较DDL的初始值: <asp:RequiredFieldValidator InitialValue="0" ID="rfvDDL_Product" Display="Dynamic" ControlToValidate="DDL_Product" runat="server" Text="*" ErrorMessage="Please Select the Product" ForeColor="Red"> </asp:RequiredFieldValidator> 编辑: 有关解释,请访问MSDN: CustomValidator Class
RequiredFieldValidator Class
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Asp.net MVC – 多语言网站
- asp.net – 用于DropDownList的MVC2 EditorTemplate
- .net – 我无法在我的网络应用程序中使log4net工作:(
- asp.net-mvc-3 – 当调用Controller.PartialView时,ASP.NET
- 如何使用ASP.NET获取访问者位置(国家,州和城市)
- asp.net-mvc – 如何设置AntiForgeryToken cookie路径
- asp.net – 从多个Web.config文件访问appSettings
- asp.net – 如何修复System.Data.SqlClient.SqlException:
- asp.net-mvc – RenderAction调用错误的动作方法
- asp.net-mvc – ASP.NET MVC应用程序的WatiN最佳实践/经验法
- asp.net-mvc – @ Html.Action in Asp.Net Core
- asp.net – 可以让CompareValidator接受带逗号的
- asp.net-mvc – 从母版页调用部分视图时的ASP.NE
- 剃刀 – 从另一个TagHelper调用TagHelpers?
- 如何最小化ASP.NET C#项目DLL大小?
- ASP.NET Core - 源码解析 - Program.cs (一)
- asp-classic – CDO电子邮件主题特殊字符错误
- asp经典 – 如何获得经典ASP中的当前域?
- asp.net – 即使在ssl上,在ViewState中存储信用卡
- UnobtrusiveJavaScriptEnabled键在.NET中的作用是