好吧,所以我完全不知道如何做到这一点。我需要做的是使用正则表达式验证文本框的某些字段和所有字段的必填字段。我已经设置好了,所以当用户单击按钮时,每次都会显示一组新的输入字段,这样如果他们有多个工作经验,他们就可以无限地输入。我试过的代码不起作用。还可能要注意,由于遇到此问题,到目前为止我只完成了地址一。一旦我弄清楚了,我就可以做剩下的了。
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# 代码中创建这些对象,所以我不知道该怎么做。