简体   繁体   English

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

[英]rangevalidator c# how to use later in code

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

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

How do I get hold of the created range validator to trigger it ie rv.validate(); 我如何握住创建的范围验证器来触发它,即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. 首先,要进行调试,我建议您检查plcMyStaff在其中添加控件的plcMyStaff对象,以查看它是否确实包含您要访问的控件。

You should be able to retrieve it from the Page object that your webform inherits. 您应该能够从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)
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM