简体   繁体   English

必填字段验证器在后退列表中消失

[英]Required Field validator disappears on dropdownlist post back

I populate two dropdownlist in asp.net. 我在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 赫玛

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. 问题是您使用InitialValue属性与列表项的value属性进行比较时应该与文本值进行比较。 They should have named the property InitialText or something... 他们应该将属性命名为InitialText或者什么......

Change your RequiredFieldValidator to the following: RequiredFieldValidator更改为以下内容:

<asp:RequiredFieldValidator ID="rfvEmp" runat="server" ControlToValidate="ddlEmp" ErrorMessage="employee" InitialValue="--Select--">
</asp:RequiredFieldValidator>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" ErrorMessage ="DropDownList1" InitialValue="--Select--">  
</asp:RequiredFieldValidator>

The client side code is comparing the value that is being displayed, not the value attached to the selection behind the scenes. 客户端代码正在比较正在显示的值,而不是附加到幕后选择的值。

Workaraund:如果所选项是默认项,则在autopostback上执行的codebehind方法上将RequiredFieldValidator1.IsValid属性设置为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM