mmedi005
asked on
When a validator control is flagged, can I change the color of a label to red instead of having a message pop out?
When a validator control is flagged, can I change the color of a label to red instead of having a message pop out?
e.g. If the web page has a label First Name and a text box with a requiredfield validtor control for it. The user submits a blank field, how can I change the label color to red instaed of a message popping up?
e.g. If the web page has a label First Name and a text box with a requiredfield validtor control for it. The user submits a blank field, how can I change the label color to red instaed of a message popping up?
ASKER
Reason why I want this is because if I set 2 different validators next to each other, the second validtor displays its message with a space to the left, where i want it to align all the way to the left. It's almost like the controlis there, but hidden, and when flagged its display is changed to true.
Do you think javascript is the only way to go?
Would I be able to get an exmple to start me off in the right direction?
Do you think javascript is the only way to go?
Would I be able to get an exmple to start me off in the right direction?
you can set the "display" property of the validator contros to "dynamic" .. default is "static" .. if made "dynamic", the space is only occupied if there is a error .. else no space is taken in the page by the validator control ..
Rejo
Rejo
This is pretty easy if you just set the style for a Label, and then set the Visible property when you need to show or hide the message.
Bob
Bob
ASKER
what event from the requiredfieldvalidtor can i use to change the labels style?
Here are examples from the Page_Load event:
C#
if (!this.IsValid && this.RequiredFieldValidato r1.IsValid )
this.Label1.CssClass = "Error";
else
this.Label1.CssClass = "Valid";
VB.NET:
If Not Me.IsValid AndAlso Me.RequiredFieldValidator1 .IsValid Then
Me.Label1.CssClass = "Error"
Else
Me.Label1.CssClass = "Valid"
End If
Bob
C#
if (!this.IsValid && this.RequiredFieldValidato
this.Label1.CssClass = "Error";
else
this.Label1.CssClass = "Valid";
VB.NET:
If Not Me.IsValid AndAlso Me.RequiredFieldValidator1
Me.Label1.CssClass = "Error"
Else
Me.Label1.CssClass = "Valid"
End If
Bob
The reason why I did not do that is because changing the style needs to be set at the server .. or even if done at the client .. you will need to call the function for each of the error .. I do the same thing using a control .. the style of the label is changed on the client in the extended control .. but there is no code involved for each page, no function to be called, but just set a property ..
mmedi005, did the display property not work for you?
mmedi005, did the display property not work for you?
ASKER
Yes the dynamic property worked, but Bob's suggestion was the direction i was going. I don't mind if the validation event is called on postback.
But using Bob's method I got...
Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate.
Any ideas?
But using Bob's method I got...
Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate.
Any ideas?
The code that Bob gave needs to be within the event that caused the postback .. maybe a button click .. I think you might have added that code in page load event ...
I am surprised that you do not mind the postback just to highlight the error .. this kind off negates the usage of validator controls, esp where in a web application scenario where we try to acheive the maximum possible on the browser before we process it on the server .. anyway, you might have your own reason for taking that approach ..
Rejo
I am surprised that you do not mind the postback just to highlight the error .. this kind off negates the usage of validator controls, esp where in a web application scenario where we try to acheive the maximum possible on the browser before we process it on the server .. anyway, you might have your own reason for taking that approach ..
Rejo
by the way, I thoguht the reason why you are doing this is because "if I set 2 different validators next to each other, the second validtor displays its message with a space to the left, where i want it to align all the way to the left. " if the dynamic property worked, then where you not satisfied with the result? just curious ..
Rejo
Rejo
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are going to get points, you helped me a lot and I am very thankful. Different ideas were thrown at me and I am trying the different suggestions. Your responses were very good, and maybe my question went the a different direction or I did explain myself right, but any how, you will both get points from me.
Thank you again. I still would like see an example of your first response.
thank you for all your help.
Thank you again. I still would like see an example of your first response.
thank you for all your help.
ASKER
If Me.IsPostBack Then
If Not Me.IsValid AndAlso Me.rfv_RequiredFieldValida tor1.IsVal id Then
Me.Label1.ForeColor = Drawing.Color.Red
Else
Me.Label1.ForeColor = Drawing.Color.Black
End If
End If
is what i put and still got the error...
If Not Me.IsValid AndAlso Me.rfv_RequiredFieldValida
Me.Label1.ForeColor = Drawing.Color.Red
Else
Me.Label1.ForeColor = Drawing.Color.Black
End If
End If
is what i put and still got the error...
ASKER
>>I did explain myself right, but any how, you will both get points from me.
I meant "did not explain"
I meant "did not explain"
That almost sounds like you don't have validation set up correctly, like not setting the ControlToValidate property on the validator.
Bob
Bob
ASKER
its set....with out the code, it would setfocus or show its errortext.
all was set up
all was set up
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I am being brain-dead today, so I am going to sit back, and clear my head before I say any more--OK?
Bob
Bob
ASKER
I have some work to do, but im gonna try to make this work tonight. thanks for all the help...ill be back posting early tomorrow. My brain is also getting fried up.
ASKER
If Not Me.IsValid AndAlso Not Me.rfv_firstname.IsValid Then
Me.lbl_first_name.ForeColo r = Drawing.Color.Red
Else
Me.lbl_first_name.ForeColo r = Drawing.Color.Black
End If
worked in the click event. Also there was a NOT missing from the if statement.
im gonna try the code later tonight when i'm done with some stuff around the office.
thanks again
Me.lbl_first_name.ForeColo
Else
Me.lbl_first_name.ForeColo
End If
worked in the click event. Also there was a NOT missing from the if statement.
im gonna try the code later tonight when i'm done with some stuff around the office.
thanks again
good .. as i said before .. you do not seem to be noticing my comments ;-) .. it will not work in page load as there is nothing to be validated and if you look the error message, that is what it says "It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate"
anyway, try the other code for the control and tell me if it works ..
Rejo
anyway, try the other code for the control and tell me if it works ..
Rejo
then i registered a clientscript using RegisterClientScriptBlock (javascript function) which loops through the Page_Validators array (available when validators are placed in a aspx page)
I registered another function using RegisterOnSubmitStatement which calls the above javascript function on submit ..
After that its just a matter of drag and drop the custom validator and setting the "LabelTohighlight" property .. the other properties like controlToValidate, errormessage etc are still available and the page consuming the control is free to set it as needed ..
Rejo