rangevalidator c#如何在稍后的代码中使用

本文关键字:代码 rangevalidator | 更新日期: 2023-09-27 18:27:22

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

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

希望你能帮助我。

谢谢,

Andy

rangevalidator c#如何在稍后的代码中使用

首先,我建议检查要在其中添加控件的plcMyStaff对象,看看它是否确实包含要访问的控件。

您应该能够从Web窗体继承的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)
{
}