In an ASP.NET Repeater Control, why is RequiredFieldValidator not working as expected on dynamically generated Dropdown?

24 Views Asked by At

I have an ASP .NET project that uses Repeater control to render controls dynamically based on data source value. Based on the value, the dropdown control will be visible only on certain criteria and a required field validator is assigned to that drop down.

Drop down is not visible for all the data rows. My problem here is, the validation is happening for all the rows even if the drop down is hidden.

Client side validation is working fine. But Server side variable Page.IsValid is false even if I have a value selected in the visible drop down.

  protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Value");


            var r1 = dt.NewRow();
            r1[0] = "User1";
            r1[1] = "1";
            dt.Rows.Add(r1);

            var r2 = dt.NewRow();
            r2[0] = "User2";
            r2[1] = "2";
            dt.Rows.Add(r2);

            var r3 = dt.NewRow();
            r3[0] = "User3";
            r3[1] = "2";
            dt.Rows.Add(r3);

            var r4 = dt.NewRow();
            r4[0] = "User4";
            r4[1] = "3";
            dt.Rows.Add(r4);

            myRepeater.DataSource = dt;
            myRepeater.DataBind();

            //Assigning a function to show alert when page is invalid.
            if (!Page.IsPostBack)
            {
                btnSubmit.Attributes.Add("onclick", "return pagevalidate()");

            }

        }

        protected bool IsVisible(object value)
        {
            return value.ToString() == "1";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                Page.RegisterStartupScript("ss", "<script>alert('valid data');</script>");
            }
            else
            {
                Page.RegisterStartupScript("ss", "<script>alert('Please provide valid data');</script>");
            }
        }
        //Tried to disable and hide the validator, but not working
        protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var control = e.Item.FindControl("validator") as RequiredFieldValidator;

            var dd = e.Item.FindControl("dropdown") as DropDownList;

            var value = $"{(e.Item.DataItem as DataRowView).Row["Value"]}";

            if (value != "1")
            {
                control.Enabled = false;
                control.Visible = false;
            }


        }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepeaterDemo.aspx.cs" Inherits="WebApplication3.RepeaterDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater OnItemDataBound="myRepeater_ItemDataBound" runat="server" ID="myRepeater">
                <ItemTemplate>
                    <div style="padding: 10px; margin: 10px; border: 1px solid #000">
                        <asp:TextBox runat="server" ID="textTextBox"
                            value='<%# Eval("Name") %>' MaxLength="350"></asp:TextBox>

                        <asp:DropDownList runat="server" ID="dropdown" Visible='<%# IsVisible(Eval("Value")) %>'>
                            <asp:ListItem Text="" />
                            <asp:ListItem Text="Apple" />
                            <asp:ListItem Text="Orange" />
                            <asp:ListItem Text="Guava" />
                        </asp:DropDownList>

                        <asp:RequiredFieldValidator ID="validator" ErrorMessage="Please enter any value" ControlToValidate="dropdown" runat="server" />
                    </div>
                </ItemTemplate>
            </asp:Repeater>
            <asp:Button id="btnSubmit" Text="Click" runat="server" OnClick="btnSubmit_Click" />
        </div>
    </form>
</body>
<script type="text/javascript">
    function pagevalidate() {
        if (typeof (Page_ClientValidate) == 'function')
            Page_ClientValidate();

        if (!Page_IsValid) {
            alert('Please correct the invalid values as highlighted.');
        }
    }
</script>
</html>

I tried to show validation alert only for the visible controls in the repeater control. It is working fine from client side validation. But in server side validation when I call Page.Validate() method Page.IsValid property value is false even if all the visible controls have value assigned.

0

There are 0 best solutions below