Asp.net 中使用必填字段验证器进行下拉列表验证
我有 Dropdownlist,其值字段和文本字段在运行时绑定。 它有 --select--
作为第一项,值为 0
其余的值在运行时绑定。
我已将控件和验证器的验证组指定为 "g1"
和 Intialvalue=0
但即使我选择 --select--
选项,页面仍然会回发。
<asp:DropDownList AutoPostBack="true" CssClass="dropdown" ValidationGroup="g1"
ID="ddlReportType" runat="server"
OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="ddlReportType" ID="RequiredFieldValidator1"
ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
InitialValue="0" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
以及绑定下拉菜单的代码
ddlReportType.Items.Clear();
ddlReportType.DataSource = dt.Tables[0];
ddlReportType.DataTextField = "ReportType";
ddlReportType.DataValueField = "ReportTypeID";
ddlReportType.DataBind();
ddlReportType.Items.Insert(0, new ListItem("--Select--", "0"));
//ddlReportType.Items[0].Value = "0";
ddlReportType.SelectedIndex = 0;
I have Dropdownlist whose value field and text field are bind at runtime.
it has --select--
as first item with a value of 0
and the rest of the values are bind at runtime.
I have given validaton group for both the control and the validator as "g1"
and Intialvalue=0
But still the page is posting back even if I select --select--
option.
<asp:DropDownList AutoPostBack="true" CssClass="dropdown" ValidationGroup="g1"
ID="ddlReportType" runat="server"
OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="ddlReportType" ID="RequiredFieldValidator1"
ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
InitialValue="0" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
And code Behind to Bind the Dropdown
ddlReportType.Items.Clear();
ddlReportType.DataSource = dt.Tables[0];
ddlReportType.DataTextField = "ReportType";
ddlReportType.DataValueField = "ReportTypeID";
ddlReportType.DataBind();
ddlReportType.Items.Insert(0, new ListItem("--Select--", "0"));
//ddlReportType.Items[0].Value = "0";
ddlReportType.SelectedIndex = 0;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里使用 asp:CompareValidator,并将值与“select”选项进行比较。
使用
Operator="NotEqual" ValueToCompare="0"
阻止用户提交“选择”。当您执行上述操作时,如果您从下拉列表中选择“select”选项,它将显示
ErrorMessage
。Here use asp:CompareValidator, and compare the value to "select" option.
Use
Operator="NotEqual" ValueToCompare="0"
to prevent the user from submitting the "select".When you do above, if you select the "select " option from dropdown it will show the
ErrorMessage
.我为此苦苦挣扎了几天,直到我不得不构建一个新的下拉菜单时偶然发现了这个问题。我有几个 DropDownList 控件并尝试进行验证,但没有成功。一个是数据绑定的,另一个是从 aspx 页面填充的。我需要删除数据绑定列表并添加第二个手动列表。在我的例子中,如果您构建了这样的下拉列表并查看所需验证器或比较验证器的任何值(0 或 -1),验证器就会失败:
但是,像这样添加初始值对于比较验证器来说会立即起作用。
I was struggling with this for a few days until I chanced on the issue when I had to build a new Dropdown. I had several DropDownList controls and attempted to get validation working with no luck. One was databound and the other was filled from the aspx page. I needed to drop the databound one and add a second manual list. In my case Validators failed if you built a dropdown like this and looked at any value (0 or -1) for either a required or compare validator:
However adding the InitialValue like this worked instantly for a compare Validator.
在必填字段验证器标记中添加
InitialValue="0"
Add
InitialValue="0"
in Required field validator tag