The Items for the lists are:
myDDL1: "", "Red" and "Blue"
myDDL2: "", "Sky" and "Royal"
If the user selects "Blue", then they should also select a value from myDDL2 (if a value is not selected, then display "Please select a value" next to myDDL2.
If the user selects "Red", then I dont need any validation on myDDL2 (or maybe I should validate that the user has not selected a value from myDDL2?)
I think that what I need is a custom validator which will check if myDDL2 selected text is "Blue" and that a value has been selected on myDDL2 , but I'm not having any luck with coding.
The aspx does not have a custom validator - instead, I would like it to be created within the aspx.cs Page_Load event. Also, I would prefer this to be a server-side validation (although I would consider using client-side).
Felipe RamosRank #1,409 · 1.2K reputationPosted Oct 11, 2010, 8:29 AM EDT
Felipe RamosRank #1,409 · 1.2K reputationPosted Oct 11, 2010, 7:39 AM EDT
Gary King77 reputationPosted Oct 11, 2010, 7:47 AM EDT
The aspx does not have a custom validator control on it (or rather, I do not create the control on the page). What I need is for the Page_Load event to create the control and then set it to validate myDDL2.
So, other than the 2 DDL's, there is nothing on the aspx.
Gary King77 reputationPosted Oct 12, 2010, 6:48 AM EDT
I would now like to improve this by making it dynamic as follows.....
I want to pass values to the custom_ServerValidate function, so that it can be used for various validations. What I would be passing is the first control (XXXX), the value to be evaluated (somevalue), the second control (YYYY) and the value of this to be evaluated (anothervalue).
So what I would end up with is multiple controls on my page (eg, DropDownLists that are created by code) and multiple CustomValidators (again, created by code) - each of which would call the same custom_ServerValidate function. The custom_ServerValidate function would then validate the values of the controls that are passed to it.
Is this possible?
protected void Page_Load(object sender, EventArgs e)
{
CustomValidator objCV = new CustomValidator();
objCV.ServerValidate += new ServerValidateEventHandler(custom_ServerValidate);
objCV.ID = "myCV";
objCV.ErrorMessage = "Custom Validator Message";
form1.Controls.Add(objCV);
}
protected void custom_ServerValidate(object source, ServerValidateEventArgs args)
{
if (XXXX.SelectedItem.Text == somevalue)
if (YYYY.SelectedItem.Text == anothervalue)
{
args.IsValid = false;
}
else
args.IsValid = true;
}
Note: the values in RED BOLD text are to be passed to the custom_ServerValidate function.
Felipe RamosRank #1,409 · 1.2K reputationPosted Oct 12, 2010, 7:36 AM EDT
Gary King77 reputationPosted Oct 12, 2010, 8:01 AM EDT
I really cannot even start to figure out how to write my own validation function. Is there really no way that I can use the custom valdiator to achieve the result that I want?
What I am trying achieve is a web page that is completely data driven (from SQL Server).
For example:
Q1 is a required field, but Q2 is only a required field if the answer to Q1 is YES. Hence me looking at the custom validator option - maybe there is a better approach to the problem?
Felipe RamosRank #1,409 · 1.2K reputationPosted Oct 12, 2010, 8:07 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts