开发者

Required Field validator disappears on dropdownlist post back

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜