Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I'm working on a project which is a kind of HRMS. I'm stuck with a problem with the Validators. In a page I've dynamically created & attached two validators , one is RangeValidator and the other one is RequiredFieldValidator to a TextBox control. The problem is When I pass "HR" for login and getting onto this page, both the validators work absolutely fine, but when I'm trying to use some other Department say-"abc" they get failed. Validation errors are being shown after the button click event code are executed. How can I restrict them to validate before the button_click event code get executed. please help me.

below is the method I use to set:

C#
public static void SetKPIs(DataTable dt, string dept, Panel pnl, Panel rfvpnl)
        {
            if (dt != null)
            {
                try
                {
                    DataRow[] EmpKPIs = dt.Select("dept='" + dept + "'");

                    foreach (DataRow dr in EmpKPIs)
                    {
                        Label lbl = new Label { ID = "lbl_" + dept + "_" + dr["kpi"].ToString(), Text = dr["kpi"].ToString() + " :", Width = new Unit(135) };
                        Label lblBlank = new Label { CssClass = "label", ID = "Min_Max_" + dept + "_" + dr["kpi"].ToString(), Text = "  (" + dr["kpi_min_weight"].ToString() + " - " + dr["kpi_max_weight"].ToString() + ")" };
                        TextBox txtbox = new TextBox { CssClass = "txtinput", Width = new Unit(30), ID = "txt" + dept + "_" + dr["kpi"].ToString() };
                        txtbox.ID = "txt" + dept + "_" + dr["kpi"].ToString();
                        
                        RangeValidator rv = new RangeValidator();
                        rv.ID = "rv" + dept + "_" + dr["kpi"].ToString();
                        rv.ControlToValidate = txtbox.ID;
                        rv.Type = ValidationDataType.Double;
                        rv.MinimumValue = dr["kpi_min_weight"].ToString();
                        rv.MaximumValue = dr["kpi_max_weight"].ToString();
                        rv.SetFocusOnError = true;                        
                        rv.Display = ValidatorDisplay.Dynamic;                        
                        rv.CssClass = "aboveBottom";
                        rv.ForeColor = System.Drawing.Color.Brown;
                        rv.Font.Size = new FontUnit(9);
                        rv.ErrorMessage = "valid_range_["+dr["kpi"].ToString()+"]:-(" + dr["kpi_min_weight"].ToString() + "-" + dr["kpi_max_weight"].ToString()+")";                        
                        rv.ToolTip = "Must be between " + dr["kpi_min_weight"].ToString() + " & " + dr["kpi_max_weight"].ToString();
                        
                        RequiredFieldValidator rfv = new RequiredFieldValidator();
                        rfv.ID = "rfv" + dept + "_" + dr["kpi"].ToString();
                        rfv.ControlToValidate = txtbox.ID;
                        rfv.ErrorMessage = dr["kpi"].ToString();
                        rfv.CssClass = "aboveBottom";
                        rfv.ForeColor = System.Drawing.Color.Brown;
                        rfv.Font.Size = new FontUnit(9);                        
                        rfv.BackColor = System.Drawing.Color.Transparent;
                        rfv.Display = ValidatorDisplay.None;

                        pnl.Controls.Add(lbl);
                        pnl.Controls.Add(new LiteralControl("   "));
                        pnl.Controls.Add(txtbox);
                        pnl.Controls.Add(lblBlank);
                        pnl.Controls.Add(new LiteralControl("   <br/><br/>"));
                        pnl.Controls.Add(rv);                        
                        rfvpnl.Controls.Add(rfv);
                    }
                }
                catch (Exception ex) { throw new ArgumentException("Error: "+ex.Message); }
                
            }

        }




I appreciate any help !

Thanks in Advance,
Sunny_K
Posted
Updated 25-Jun-12 1:54am
v2
Comments
Vani Kulkarni 25-Jun-12 7:46am    
Can you post your code, adding the validators and attaching them to the button or textbox?
Sunny_Kumar_ 25-Jun-12 7:54am    
sure, updated. Please have a look.

Hi ,
What about the validation group Make sure you are handled
Google[^]
Best Regards
M.Mitwalli
 
Share this answer
 
Use ValidationGroup Proprety of the textbox and the validation controls and the button .
Set all the ValidationGroup name same .
 
Share this answer
 
Comments
Sunny_Kumar_ 25-Jun-12 8:15am    
can you please expose some sample on that what do you mean exactly as I've already tried with Grouping them.
bhagirathimfs 25-Jun-12 9:15am    
<asp:TextBox ID="txtDate" Height="15px" ValidationGroup="test" runat="server" MaxLength="3" Width="40" AutoPostBack="true"
CausesValidation="true">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="test" runat="server" ErrorMessage="*"
ControlToValidate="txtDate" Display="Dynamic">
<asp:RegularExpressionValidator ID="revDate" ValidationGroup="test" runat="server" ControlToValidate="txtDate"
Display="Dynamic" ValidationExpression="^\d+$" ErrorMessage="Enter a Valid Number.">
<Asp:Button Id="btnSubmit" runat="server" ValidationGroup="test" Text="Submit">

I want to say this
Sunny_Kumar_ 26-Jun-12 0:19am    
doesn't work.
bhagirathimfs 26-Jun-12 1:32am    
Have you set the ValidationGroup Property of button .
Means:<Asp:Button Id="btnSubmit" runat="server" ValidationGroup="test" Text="Submit">

The ValidationGroup name should be same for all cases.
Like here test.
You need to have some button which triggers postback event and also you need to embed all these in validation groups.
 
Share this answer
 
Comments
Sunny_Kumar_ 25-Jun-12 8:04am    
kishhr, Validators always get fired on a postback that I do know. The problem is they're not getting fired after doing a postback but after that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900