必填字段验证器在下拉列表回发时消失
我在 asp.net 中填充两个下拉列表。 两者都分配给必填字段验证器。
隐藏代码如下
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("emp");
dt.Columns.Add("ename");
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["emp"] = (i + 1).ToString();
dr["ename"] = (i + 1).ToString();
dt.Rows.Add(dr);
}
ddlEmp.DataSource = dt;
ddlEmp.DataTextField = "emp";
ddlEmp.DataValueField = "ename";
ddlEmp.DataBind();
ListItem l1 = new ListItem("--Select--", "0");
ddlEmp.Items.Insert(0, l1);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "emp";
DropDownList1.DataValueField = "ename";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, l1);
}
设计器代码如下
<asp:DropDownList ID="ddlEmp" AutoPostBack="true" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvEmp" runat="server" ControlToValidate="ddlEmp" ErrorMessage ="employee" InitialValue="0">
</asp:RequiredFieldValidator>
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" ErrorMessage ="DropDownList1" InitialValue="0">
</asp:RequiredFieldValidator>
<asp:Button ID="btn" runat="server" CausesValidation="true" />
现在发生的情况是,当我选择一个字段,然后再次选择“-- Select--”时,验证器出现并消失。
为什么验证者不留下来? 我哪里出错了?
盒马
I populate two dropdownlist in asp.net.
Both are assigned to a required field validator.
The codebehind is as below
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("emp");
dt.Columns.Add("ename");
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["emp"] = (i + 1).ToString();
dr["ename"] = (i + 1).ToString();
dt.Rows.Add(dr);
}
ddlEmp.DataSource = dt;
ddlEmp.DataTextField = "emp";
ddlEmp.DataValueField = "ename";
ddlEmp.DataBind();
ListItem l1 = new ListItem("--Select--", "0");
ddlEmp.Items.Insert(0, l1);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "emp";
DropDownList1.DataValueField = "ename";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, l1);
}
the designer code is as below
<asp:DropDownList ID="ddlEmp" AutoPostBack="true" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvEmp" runat="server" ControlToValidate="ddlEmp" ErrorMessage ="employee" InitialValue="0">
</asp:RequiredFieldValidator>
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" ErrorMessage ="DropDownList1" InitialValue="0">
</asp:RequiredFieldValidator>
<asp:Button ID="btn" runat="server" CausesValidation="true" />
Now what happens is when I choose a field, and then once again go and choose"-- Select--", the validator appears and disappears.
Why doesnt the validator stay?
Where am I going wrong?
Hema
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题已经困扰了我很多次,只是因为在我看来他们的设计方式有点古怪。
问题是您在应该与文本值进行比较时使用
InitialValue
属性来与列表项的 value 属性进行比较。他们应该命名属性InitialText
或其他内容...将您的
RequiredFieldValidator
更改为以下内容:客户端代码正在比较正在显示的值,而不是附加的值到幕后的选择。
This issue has bitten me a bunch of times and just because it's a little wacky how they designed it in my opinion.
The problem is your using the
InitialValue
property to compare to the value property of the list item when it should be comparing to the text value. They should have named the propertyInitialText
or something...Change your
RequiredFieldValidator
to the following:The client side code is comparing the value that is being displayed, not the value attached to the selection behind the scenes.
Workaraund:如果所选项目是默认项目,则在自动回发时执行的代码隐藏方法上,将RequiredFieldValidator1.IsValid 属性设置为 false。
Workaraund: on the codebehind method that is executed on autopostback if the selected item is the default item set the RequiredFieldValidator1.IsValid attribute to false.