必填字段验证器在下拉列表回发时消失

发布于 2024-08-28 11:43:35 字数 1767 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明明#如月 2024-09-04 11:43:37

这个问题已经困扰了我很多次,只是因为在我看来他们的设计方式有点古怪。

问题是您在应该与文本值进行比较时使用 InitialValue 属性来与列表项的 value 属性进行比较。他们应该命名属性 InitialText 或其他内容...

将您的 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>

客户端代码正在比较正在显示的值,而不是附加的值到幕后的选择。

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 property InitialText or something...

Change your RequiredFieldValidator to the following:

<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.

雪若未夕 2024-09-04 11:43:37

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文