验证动态文本框和标签

时间:2012-09-24 13:08:22

标签: c# asp.net validation dynamic

好吧所以我完全迷失了如何做到这一点。我需要做的是使用正则表达式验证文本框的所有内容和必填字段。我设置了它,所以当用户点击按钮时,每次都会显示一组新的输入字段,这样如果他们有多个工作经验,他们就可以无限地使用它。我试过的代码不起作用。也可能要注意到,由于遇到了这个问题,我到目前为止只完成了一个地址。一旦我弄明白,我将能够完成剩下的工作。

ASP.net代码只是一个按钮。

C#代码:

protected void Page_Load(object sender, EventArgs e)
{
    // Add any controls that have been previously added dynamically

    for (int i = 0; i < TotalNumberAdded; ++i)
    {
        addcontrolsemployment(i + 1);
    }




}


private void addcontrolsemployment(int controlNumber)
    {


        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_" + controlNumber;
        addressTextbox.ID = "AddressTextbox_" + controlNumber;
        phoneTextbox.ID = "phoneTextbox_" + controlNumber;
        fromDateTextbox.ID = "fromDateTextbox_" + controlNumber;
        toDateTextbox.ID = "toDateTextbox_" + controlNumber;
        supervisorTextbox.ID = "supervisorTextbox_" + controlNumber;
        jobTitleTextbox.ID = "jobTitleTexbox_" + controlNumber;
        dutiesTextbox.ID = "dutiesTextbox_" + controlNumber;
        dutiesTextbox.Width = 250;
        hoursTextbox.ID = "hoursTexbox_" + controlNumber;
        wageTextbox.ID = "wageTexbox_" + controlNumber;
        leavingTextbox.ID = "leavingTexbox_" + controlNumber;
        leavingTextbox.Width = 250;
        //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 addressRequired = new RequiredFieldValidator();
        addressRequired.Text = "Address is Required";
        addressRequired.ErrorMessage = " Please Enter less than 50 characters for address";
        addressRequired.ID = "addressRequired" + controlNumber; addressRequired.ControlToValidate = addressTextbox.Text;
        addressRegEx.ValidationExpression = ".{0,50}"; 
        addressRegEx.ControlToValidate = addressTextbox.Text;



        // add the label and textbox to the panel, then add the panel to the form
        newPanel.Controls.Add(new LiteralControl("<table><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'>"));
        newPanel.Controls.Add(employerTextbox);
        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(addressRegEx); newPanel.Controls.Add(addressRequired);
        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("</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("</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("</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("</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("</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);

        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("</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("</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);
        newPanel.Controls.Add(new LiteralControl("</td></tr></table>"));
        form1.Controls.Add(newPanel);

    }

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


        // Increase the number added and add the new label and textbox
        TotalNumberAdded++;
        addcontrolsemployment(TotalNumberAdded);

    }

更新:我遇到的一个主要问题是我希望它在显示下一个表单之前按下按钮进行验证。但我无法通过这些物品,我可能会愚蠢,但我不知道该怎么做。就像我在ASP.net页面中拥有对象一样,这不是问题,但由于我在c#代码中创建它们,我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

我的建议是为您要检查的每个控件分配一个唯一的名称

在PostBack中,您应该能够系统地检查through Request.Form

另一种方法是逐个添加条目,只提供一个带有“添加”按钮的输入面板。当用户添加条目时,会将其发送到服务器,验证并保存在会话中,然后将保存的条目显示给用户。

这种方法可以更容易验证,但显然你有更多的往返服务器。 Ajax可以帮助减少开销。