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