我在 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--”时,验证器出现又消失。
为什么验证人不留下来?我哪里错了?
盒马