I created a range validator and would like to trigger it once the submit button was clicked.

RangeValidator rv_tbAbsenceDay = new RangeValidator();
rv_tbAbsenceDay.ID = "rv_tbAbsenceDay" + tbAbsenceDay.ID;
rv_tbAbsenceDay.ControlToValidate = tbAbsenceDay.ID;
rv_tbAbsenceDay.EnableClientScript = true;
rv_tbAbsenceDay.Display = ValidatorDisplay.Dynamic;
rv_tbAbsenceDay.MinimumValue = DateTime.Now.AddMonths(-6).ToString("d");
rv_tbAbsenceDay.MaximumValue = DateTime.Now.ToString("d");
rv_tbAbsenceDay.ErrorMessage = "Date cannot be older than 6 months and not in the future.";
rv_tbAbsenceDay.SetFocusOnError = true;
plcMyStaff.Controls.Add(rv_tbAbsenceDay);

plcMyStaff is a placeholder.

<asp:PlaceHolder ID="plcMyStaff" runat="server"></asp:PlaceHolder>

How do I get hold of the created range validator to trigger it i.e. rv.validate(); ?

I have tried this:

protected void MarkAsSick_Command(Object sender, CommandEventArgs e)
{
    DropDownList tempddlReason = (DropDownList)plcMyStaff.FindControl("ddlReason" + e.CommandArgument.ToString());
    TextBox temptbAbsenceDay = (TextBox)plcMyStaff.FindControl("tbAbsenceDay" + e.CommandArgument.ToString());
    TextBox temptbLastDayWorked = (TextBox)plcMyStaff.FindControl("tbLastDayWorked" + e.CommandArgument.ToString());
    RangeValidator temprv_tbAbsenceDay = (RangeValidator)plcMyStaff.FindControl("rv_tbAbsenceDay" + e.CommandArgument.ToString());
    temprv_tbAbsenceDay.validate();
...

Hope you can help me.

thanks,

Andy

有帮助吗?

解决方案

First off to debug this I would suggest examining the plcMyStaff object in which you are adding the control to see if it does in fact contain the control you wish to access.

You should be able to retrieve it from the Page object that your webform inherits.

Page.FindControl();

// Or you can Iterate through each control to see what the control is called and test for the name you want
foreach (var control in Page.Controls)
{

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top