rangevalidator c#如何在代码中使用

时间:2013-08-12 10:02:18

标签: c# rangevalidator

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

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

希望你能帮助我。

感谢,

安迪

1 个答案:

答案 0 :(得分:0)

首先要调试这个我建议检查你添加控件的plcMyStaff对象,看看它是否确实包含你想要访问的控件。

您应该能够从您的webform继承的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)
{

}