Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42225

VS 2008 create one RequiredFieldValidator and use it for many textboxes?

$
0
0
I am working on a web page that dynamically creates a table with rows containing textboxes. It looks like a gridview. I actually thought the page used a gridview until I dug into the code, so here I am maintaining a table.

DOB is a required field. I want to validate it without a postback. DOB is made up of three textboxes: Month, Day and Year.

So let's say you are entering new data for three people. You can indicate this on the page, and you will get three blank rows in your table. So I need to create the validators when I create the rows with the textboxes (if not, tell me I am wrong or if there is a better way).

Following how the textboxes are created, I wanted to do this:

Code:

                // Experimenting with required field validator
                RequiredFieldValidator validateYr = new RequiredFieldValidator();
                validateYr.ID = REQUIRED_FIELD_VALIDATOR_ID_BASE + "_year" + currentSubscriberIDAsString;
                validateYr.ControlToValidate = birthYearBox.ID;
                validateYr.ErrorMessage = "Date of birth is a required field";
                this.Controls.Add(validateYr);
               
                RequiredFieldValidator validateMo = new RequiredFieldValidator();
                validateMo.ID = REQUIRED_FIELD_VALIDATOR_ID_BASE + "_month" + currentSubscriberIDAsString;
                validateMo.ControlToValidate = birthMonthBox.ID;
                validateMo.ErrorMessage = "Date of birth is a required field";
                this.Controls.Add(validateMo);
               
                RequiredFieldValidator validateDay = new RequiredFieldValidator();
                validateDay.ID = REQUIRED_FIELD_VALIDATOR_ID_BASE + "_day" + currentSubscriberIDAsString;
                validateDay.ControlToValidate = birthDayBox.ID;
                validateDay.ErrorMessage = "Date of birth is a required field";
                this.Controls.Add(validateDay);

What I want to know is what is the best way to report the errors? In other words, where on the page should the validator(s) be positioned? There is the potential for a lot of blank rows, and therefore validators, to be created at once so I don't want them all over the place. I'd like just a placeholder maybe where I can report one required DOB missing, because I think if the user saw one but had more, he would realize he had to correct many.

Please don't tell me to use a gridview or use a calendar control or anything else that trust me I would have used had I been the original author. This is old code and there is barely enough time to do the required enhancements, so do anything extra is out of the question.

Thanks!

Viewing all articles
Browse latest Browse all 42225


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>