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 RamosPosted Oct 11, 2010, 12:29 PM
Felipe RamosPosted Oct 12, 2010, 12:07 PM
Gary KingPosted Oct 12, 2010, 12:01 PM
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 RamosPosted Oct 12, 2010, 11:36 AM
Gary KingPosted Oct 12, 2010, 10:48 AM
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.
Gary KingPosted Oct 11, 2010, 11:47 AM
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.
Felipe RamosPosted Oct 11, 2010, 11:39 AM