1

我创建了一个范围验证器,并希望在单击提交按钮后触发它。

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 是一个占位符。

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

如何获取创建的范围验证器来触发它,即rv.validate();

我试过这个:

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();
...

希望您能够帮助我。

谢谢,

安迪

4

1 回答 1

0

首先要对此进行调试,我建议plcMyStaff您检查要在其中添加控件的对象,以查看它是否确实包含您希望访问的控件。

您应该能够从Page您的网络表单继承的对象中检索它。

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)
{

}
于 2013-08-12T10:15:13.190 回答