必填字段校验器上的DropDownList消失后回 [英] Required Field validator disappears on dropdownlist post back

查看:132
本文介绍了必填字段校验器上的DropDownList消失后回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我填充asp.net 2 DropDownList中。
两者都分配到一个必填字段验证器。

I populate two dropdownlist in asp.net. Both are assigned to a required field validator.

在codebehind是如下

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);

        }

设计师code是如下

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?

喝骂

推荐答案

这个问题已经咬伤了我一堆倍,只是因为它是一个有点古怪,他们如何设计它在我看来。

This issue has bitten me a bunch of times and just because it's a little wacky how they designed it in my opinion.

问题是您使用与InitialValue 属性比较列表项的值属性时,应当比较文本值。他们应该有一个名为属性 InitialText 或东西...

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

更改的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>

的客户端code为比较正在显示,不附着在幕后的选择的值的值。

The client side code is comparing the value that is being displayed, not the value attached to the selection behind the scenes.

这篇关于必填字段校验器上的DropDownList消失后回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆