2

好吧,我已经处理这个问题两天了,我还没有解决这个问题。我想用 RegularExpressionValidation 验证文本框。

  1. 用户从 Dropbox 中选择是或否
  2. 如果是,则他们单击按钮以打开输入表单
  3. 填写 texbox,并非全部都是必需的,但如果他们确实填写了日期,那么它必须采用正确的格式。
  4. 如果有效,向导上的下一步按钮将再次可见,并且将显示一组新的文本框。注意:下一个按钮出现的唯一时间是一切都有效。

我的主要问题是我希望它只验证这个向导步骤而不是整个“页面”

每当我单击按钮时,我都会不断收到错误消息,但它可能来自正则表达式。证实():

System.NullReferenceException:对象引用未设置为对象的实例。

Source Error:


Line 590:
Line 591:     
Line 592:        addressRegEx.Validate(); <---Whats in red
Line 593:        EmployerRequired.Validate();
Line 594:        phoneRegex.Validate();

C#:

protected void Page_Load(object sender, EventArgs e)

{ 
for (int i = 0; i < TotalNumberAddedEmployment; ++i)
    {
        addcontrolsemployment(i + 1);
    }
    if (Wizard1.ActiveStep == WizardStep3)
    {

        Button nxtButton = (Button)Wizard1.FindControl("StepNavigationTemplateContainerID$StepNextButton");
        nxtButton.Visible = false;
    }

}
public void addcontrolsemployment(int controlnumberEmp)
{



    var newPanel = new Panel();


    var employerLabel = new Label();
    var addressLabel = new Label();
    var phoneLabel = new Label();
    var fromDateLabel = new Label();
    var toDateLabel = new Label();
    var supervisorLabel = new Label();
    var jobTitleLabel = new Label();
    var dutiesLabel = new Label();
    var hoursLabel = new Label();
    var wageLabel = new Label();
    var leavingLabel = new Label();

    var employerTextbox = new TextBox();
    var addressTextbox = new TextBox();
    var phoneTextbox = new TextBox();
    var fromDateTextbox = new TextBox();
    var toDateTextbox = new TextBox();
    var supervisorTextbox = new TextBox();
    var jobTitleTextbox = new TextBox();
    var dutiesTextbox = new TextBox();
    var hoursTextbox = new TextBox();
    var wageTextbox = new TextBox();
    var leavingTextbox = new TextBox();




    // textbox needs a unique id to maintain state information
    employerTextbox.ID = "EmployerTextBox_" + controlnumberEmp;
    employerTextbox.Width = 250;
    //employerTextbox.Text = "Enter Employer Here";

    addressTextbox.ID = "AddressTextbox_" + controlnumberEmp;
    addressTextbox.AutoPostBack = true;
    phoneTextbox.ID = "phoneTextbox_" + controlnumberEmp;
    phoneTextbox.AutoPostBack = true;
    fromDateTextbox.ID = "fromDateTextbox_" + controlnumberEmp;
    toDateTextbox.ID = "toDateTextbox_" + controlnumberEmp;
    toDateTextbox.AutoPostBack = true;
    supervisorTextbox.ID = "supervisorTextbox_" + controlnumberEmp;
    supervisorTextbox.AutoPostBack = true;
    jobTitleTextbox.ID = "jobTitleTexbox_" + controlnumberEmp;
    jobTitleTextbox.AutoPostBack = true;
    dutiesTextbox.ID = "dutiesTextbox_" + controlnumberEmp;
    dutiesTextbox.Width = 250;
    dutiesTextbox.AutoPostBack = true;
    hoursTextbox.ID = "hoursTexbox_" + controlnumberEmp;
    hoursTextbox.AutoPostBack = true;
    wageTextbox.ID = "wageTexbox_" + controlnumberEmp;
    wageTextbox.AutoPostBack = true;
    leavingTextbox.ID = "leavingTexbox_" + controlnumberEmp;
    leavingTextbox.Width = 250;
    leavingTextbox.AutoPostBack = true; 
    //Label text
    employerLabel.Text = "Employer: ";
    addressLabel.Text = "Address: ";
    phoneLabel.Text = "Phone #: ";
    toDateLabel.Text = "To Date: ";
    fromDateLabel.Text = "From Date: ";
    supervisorLabel.Text = "Supervisor: ";
    jobTitleLabel.Text = "Job Title: ";
    dutiesLabel.Text = "Major Duties: ";
    hoursLabel.Text = "Hours Per Week:";
    wageLabel.Text = "Final Wage: ";
    leavingLabel.Text = "Reason for Leaving: ";

    //Regular Expression and Required field validators

    var addressRegEx = new RegularExpressionValidator();
    var EmployerRequired = new RequiredFieldValidator();
    var phoneRegex = new RegularExpressionValidator();
    var toDateRegex = new RegularExpressionValidator();
    var fromDateRegex = new RegularExpressionValidator();
    var supervisorRegex = new RegularExpressionValidator();
    var jobTitleRegex = new RegularExpressionValidator();
    var dutiesRegex = new RegularExpressionValidator();
    var hoursRegex = new RegularExpressionValidator();
    var wageRegex = new RegularExpressionValidator();
    var leavingRegex = new RegularExpressionValidator();




    EmployerRequired.Text = "Enter NA if No History";
    EmployerRequired.ID = "employerRequired" + controlnumberEmp;
    EmployerRequired.ControlToValidate = employerTextbox.ID;
    EmployerRequired.ValidationGroup = "EmploymentValid";



    addressRegEx.ValidationExpression = ".{0,50}";
    addressRegEx.Text = "Please Enter less than 50 characters for address";
    addressRegEx.ErrorMessage = "Please Enter less than 50 characters for address";
    addressRegEx.ControlToValidate = addressTextbox.ID;
   //addressRegEx.ValidationGroup = "EmploymentValid";

    phoneRegex.ID = "phoneRegex" + controlnumberEmp;
    phoneRegex.Text = "Please enter in (###)###-####";
    phoneRegex.ControlToValidate = phoneTextbox.ID;
    phoneRegex.ValidationExpression = @"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}";
   // phoneRegex.ValidationGroup = "EmploymentValid";

    toDateRegex.ID = "toDateRegex" + controlnumberEmp;
    toDateRegex.Text = "Please enter in MM/DD/YYY form";
    toDateRegex.ValidationExpression=@"^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$";
    toDateRegex.ControlToValidate = toDateTextbox.ID;
   // toDateRegex.ValidationGroup = "EmploymentValid";

    fromDateRegex.ID = "fromDateRegex" + controlnumberEmp;
    fromDateRegex.Text = "Please enter in MM/DD/YYY form";
    fromDateRegex.ValidationExpression=@"^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$";
    fromDateRegex.ControlToValidate = fromDateTextbox.ID;
   // fromDateRegex.ValidationGroup = "EmploymentValid";

    supervisorRegex.ID = "supervisorRegex" + controlnumberEmp;
    supervisorRegex.Text = "Please enter in 20 characters or less";
    supervisorRegex.ValidationExpression = ".{0,20}";
    supervisorRegex.ControlToValidate = supervisorTextbox.ID;
   // supervisorRegex.ValidationGroup = "EmploymentValid";

    jobTitleRegex.ID = "jobTitleRegex" + controlnumberEmp;
    jobTitleRegex.Text = "Please enter 30 characters or less";
    jobTitleRegex.ValidationExpression = ".{0,30}";
    jobTitleRegex.ControlToValidate = jobTitleTextbox.ID;
   // jobTitleRegex.ValidationGroup = "EmploymentValid";

    dutiesRegex.ID = "dutiesRegex" + controlnumberEmp;
    dutiesRegex.Text = "Please enter 60 characters or less";
    dutiesRegex.ValidationExpression = ".{0,60}";
    dutiesRegex.ControlToValidate = dutiesTextbox.ID;
   // dutiesRegex.ValidationGroup = "EmploymentValid";

    hoursRegex.ID = "hoursRegex" + controlnumberEmp;
    hoursRegex.Text = "Please enter up to 3 digits";
    hoursRegex.ValidationExpression = "[0-9]{0,3}";
    hoursRegex.ControlToValidate = hoursTextbox.ID;
  //  hoursRegex.ValidationGroup = "EmploymentValid";

    wageRegex.ID = "wageRegex" + controlnumberEmp;
    wageRegex.Text = "Please enter in currency format";
    wageRegex.ValidationExpression = @"(?n:(^\$?(?!0,?\d)\d{1,3}(?=(?<1>,)|(?<1>))(\k<1>\d{3})*(\.\d\d)?)$)";
    wageRegex.ControlToValidate = wageTextbox.ID;
   // wageRegex.ValidationGroup = "EmploymentValid";

    leavingRegex.ID = "leavingRegex" + controlnumberEmp;
    leavingRegex.Text = "Please enter 60 characters or less";
    leavingRegex.ValidationExpression = ".{0,60}";
    leavingRegex.ControlToValidate = leavingTextbox.ID;
  //  leavingRegex.ValidationGroup = "EmploymentValid";




    // add the label and textbox to the panel, then add the panel to the form
    newPanel.Controls.Add(new LiteralControl("<br />"));

    //newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(new LiteralControl("<table border='2px'><tr>"));
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(new LiteralControl("<td class='title-text'  >"));
    newPanel.Controls.Add(employerLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px' colspan='2'>"));
    newPanel.Controls.Add(employerTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(EmployerRequired);

    newPanel.Controls.Add(new LiteralControl("</td></tr><tr><td class='title-text' >"));
    newPanel.Controls.Add(addressLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(addressTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(addressRegEx);
    newPanel.Controls.Add(new LiteralControl("</td>"));

    newPanel.Controls.Add(new LiteralControl("<td class='title-text'>"));
    newPanel.Controls.Add(phoneLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(phoneTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(phoneRegex);
    newPanel.Controls.Add(new LiteralControl("</td></tr><tr><td class='title-text'>"));
    newPanel.Controls.Add(fromDateLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(fromDateTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(fromDateRegex);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(toDateLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(toDateTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(toDateRegex);
    newPanel.Controls.Add(new LiteralControl("</td></tr><tr><td class='title-text'>"));
    newPanel.Controls.Add(supervisorLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(supervisorTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(supervisorRegex);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(jobTitleLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(jobTitleTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(jobTitleRegex);
    newPanel.Controls.Add(new LiteralControl("</td></tr><tr><td class='title-text'>"));
    newPanel.Controls.Add(dutiesLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text' colspan='2'>"));
    newPanel.Controls.Add(dutiesTextbox);//next to it
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(dutiesRegex);
    newPanel.Controls.Add(new LiteralControl("</td></tr><tr><td class='title-text'>"));
    newPanel.Controls.Add(hoursLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(hoursTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(hoursRegex);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(wageLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
    newPanel.Controls.Add(wageTextbox);
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(hoursRegex);
    newPanel.Controls.Add(new LiteralControl("</td></tr><tr><td class='title-text'>"));
    newPanel.Controls.Add(leavingLabel);
    newPanel.Controls.Add(new LiteralControl("</td><td class='title-text' colspan='2'>"));
    newPanel.Controls.Add(leavingTextbox); // next to it
    newPanel.Controls.Add(new LiteralControl("<br />"));
    newPanel.Controls.Add(leavingRegex);
    newPanel.Controls.Add(new LiteralControl("</td></tr></table><br /><hr>"));



    addressRegEx.Validate();
    EmployerRequired.Validate();
    phoneRegex.Validate();
    toDateRegex.Validate();
    fromDateRegex.Validate(); 
    supervisorRegex.Validate();
    jobTitleRegex.Validate();
    dutiesRegex.Validate();
    hoursRegex.Validate();
    wageRegex.Validate();
    leavingRegex.Validate();
    if((EmployerRequired.IsValid) && (addressRegEx.IsValid) && (phoneRegex.IsValid) && (toDateRegex.IsValid) && (toDateRegex.IsValid)  && (fromDateRegex.IsValid)&& (supervisorRegex.IsValid)&&(jobTitleRegex.IsValid)&&(dutiesRegex.IsValid)&&(hoursRegex.IsValid)&&(wageRegex.IsValid) && (leavingRegex.IsValid))
   {

    EmploymentPlaceHolder.Controls.Add(newPanel);

   }

}

protected int TotalNumberAddedEmployment
{
    get { return (int)(ViewState["TotalNumberAddedEmployment"] ?? 0); }
    set { ViewState["TotalNumberAddedEmployment"] = value; }
}
protected void EmploymentButton_Click(object sender, EventArgs e)
{




        Page.Validate();

        if (Page.IsValid)
        {
            Button nxtButton = (Button)Wizard1.FindControl("StepNavigationTemplateContainerID$StepNextButton");
            nxtButton.Visible = false;
            return;

        }
        else
        {
            // Increase the number added and add the new label and textbox
            TotalNumberAddedEmployment++;
            addcontrolsemployment(TotalNumberAddedEmployment);

            Button nxtButton =       (Button)Wizard1.FindControl("StepNavigationTemplateContainerID$StepNextButton");
            nxtButton.Visible = true;
        }



}
protected void ExperienceDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
   if (ExperienceDropDownList.SelectedValue == "Yes")
    {
        Button nxtButton = (Button)Wizard1.FindControl("StepNavigationTemplateContainerID$StepNextButton");
        nxtButton.Visible = false;
        addcontrolsemployment(1);


    }
    else 
    {

        Button nxtButton = (Button)Wizard1.FindControl("StepNavigationTemplateContainerID$StepNextButton");
        nxtButton.Visible = true;

   }


}
4

1 回答 1

1

看看这篇文章创建动态用户友好的基于 Web 的数据收集表单我想你会找到你的问题的答案。

于 2012-10-31T19:30:16.517 回答