0

Alright so i am completely lost on how to do this. What i need to do is validate the textbox's with regular expression for some and required field for all of them. I have it set up so when the user clicks the button a new set of input fields show up every time, so that if they have more than one job experience they can put it in infinitely. The code i tried won't work. Also may want to note that I have only done the address one so far due to running into this issue. Once i figure that out i will be able to do the rest.

The ASP.net code is just a button.

C# Code:

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

    }

Update: One main issue i'm having is i want it to validate on the button click before the next form is shown. But i can not pass the objects, I may be dumb but i'm not sure how to do it. Like if i were to have the objects in the ASP.net page it would be no issue but since i am creating these in the c# code i'm not sure how to do so.

user1574685
  • 181
  • 1
  • 2
  • 11
  • Use same css class for all the textboxes that you want to validate. Now put jQuery validation code for that css class. –  Sep 24 '12 at 13:14
  • check this: http://stackoverflow.com/questions/1854556/check-if-inputs-are-empty-using-jquery and http://api.jquery.com/class-selector/ –  Sep 24 '12 at 13:21
  • @DarshanJoshi: js validation is not a replacement for server side validation. it can easily be tricked, so you it can only work as a help to provide quicker feedback. – magnattic Sep 24 '12 at 13:23
  • @atticae: Yes you are right. It`s for quicker feedback. –  Sep 24 '12 at 13:27

1 Answers1

1

My advice would be to assign a unique name to every control you want to check.

In the PostBack you should then be able to check those systematically through Request.Form.

Another approach would be to add entries one by one, providing only one input panel with an "add" button. When the user adds an entry, it is sent to the server, validated and saved in the Session and the saved entries are then displayed to the user.

This approach makes it far easier to validate, but obviously you have more round trips to the server. Ajax could help reduce overhead here.

Community
  • 1
  • 1
magnattic
  • 12,638
  • 13
  • 62
  • 115
  • i thought about it...ill take all your suggestions into consideration, i kinda got it to work, just the error message is not showing up now – user1574685 Sep 24 '12 at 13:41