locked
Autopostback for dropdown failing validation inside gridview RRS feed

  • Question

  • User-1971168174 posted

    Hi,

    Validation works fine with autopostback=false but doesnt works with autopostback=true for dropdown inside template control.pl help

    My Code

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <script type="text/javascript">
                //Function to disable validator
                function disableValidator(rfvctrl, objdrpdwn) {
                    var e = document.getElementById(objdrpdwn);
                    var selval = e.options[e.selectedIndex].value;
                    if(selval =="1")
                    {
                        ValidatorEnable(document.getElementById(rfvctrl), true);
                    }
                }
            </script>
            <asp:GridView ID="GridView1" runat="server" DataKeyNames="Price" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlYes" runat="server" CssClass="max-width5" AutoPostBack="true" onchange="disableValidator()" OnSelectedIndexChanged="ddlYes_SelectedIndexChanged">
                                <asp:ListItem Value="0">No</asp:ListItem>
                                <asp:ListItem Value="1">Yes</asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" MaxLength="100" PlaceHolder="ID No" ValidationGroup="hz"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="TextBox1" Enabled="false" ErrorMessage="*"  SetFocusOnError="True" Display="Dynamic" ValidationGroup="hz"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            
        </form>
    </body>
    </html>
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    //Assign the data to GridView
                    GridView1.DataSource = this.Get_Details();
                    //Bind the Grid
                    GridView1.DataBind();
                }
    
            }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    RequiredFieldValidator rfvvalidator = (RequiredFieldValidator)e.Row.FindControl("RequiredFieldValidator6");
                    DropDownList drpdwnctrl = (DropDownList)e.Row.FindControl("ddlYes");
                    drpdwnctrl.Attributes.Add("onchange", "javascript: disableValidator('" + rfvvalidator.ClientID + "','" + drpdwnctrl.ClientID + "');");
                }
            }
            public DataTable Get_Details()
            {
                DataTable dt = new DataTable();
                DataRow dr;
                string Price = "15.123,25.123,35.3245345,45.567,55.345,15.123,25.123,35.3245345,45.567,55.345,15.123,25.123,35.3245345,45.567,55.345";
                string ContactName = "Name1,Name2,Name3,Name4,Name5,Name1,Name2,Name3,Name4,Name5,Name1,Name2,Name3,Name4,Name5";
                string City = "City1,City2,City3,City4,City5,City1,City2,City3,City4,City5,City1,City2,City3,City4,City5";
                string Country = "Country1,Country2,Country3,Country4,Country5,Country1,Country2,Country3,Country4,Country5,Country1,Country2,Country3,Country4,Country5";
    
                string[] list1 = ContactName.Split(',');
                string[] list2 = City.Split(',');
                string[] list3 = Country.Split(',');
                string[] list4 = Price.Split(',');
    
                dt.Columns.Add("Price", typeof(Double));
                dt.Columns.Add("City", typeof(string));
                dt.Columns.Add("STATUS", typeof(string));
                dt.Columns.Add("LOCATIONID", typeof(string));
                dt.Columns.Add("Date", typeof(DateTime));
    
                for (int i = 0; i < list1.Length; i++)
                {
                    dr = dt.NewRow();
                    dr["Price"] = list4[i];
                    dr["City"] = list2[i];
                    dr["STATUS"] = list3[i];
                    dr["LOCATIONID"] = list4[i];
                    dr["Date"] = DateTime.Now;
                    dt.Rows.Add(dr);
                }
                return dt;
            }
    
            protected void ddlYes_SelectedIndexChanged(object sender, EventArgs e)
            {
            }
        }


    Thanks!

    Tuesday, November 10, 2020 8:16 AM

Answers

  • User-189459990 posted

    Have you set property "CausesValidation". It works fine in my test.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2020 1:45 AM

All replies

  • User-189459990 posted

    You can set CausesValidation="true", and add the DropDownList to the ValidationGroup "hz".

    Modify DropDownList as followed.

    <asp:DropDownList ID="ddlYes" runat="server" CssClass="max-width5" 
    AutoPostBack="true" CausesValidation="true" ValidationGroup="hz" onchange="disableValidator()" OnSelectedIndexChanged="ddlYes_SelectedIndexChanged"> <asp:ListItem Value="0">No</asp:ListItem> <asp:ListItem Value="1">Yes</asp:ListItem> </asp:DropDownList>
    Wednesday, November 11, 2020 5:59 AM
  • User-1971168174 posted

    It doesnt work even after adding the DropDownList to the ValidationGroup "hz"

    Wednesday, November 11, 2020 2:33 PM
  • User-189459990 posted

    Have you set property "CausesValidation". It works fine in my test.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2020 1:45 AM