How to use the Validator controls with controls other than TextBox? - c#

I want to use the Validator control to validate a FCKEditor rich text control. Is there a way to do this on either client and/or server side?
And a broader question, is there a way to use the Validator controls for anything other than text boxes?

You can use CustomValidator control to perform custom validation logic. This control actually doesn't even require ControlToValidate to be set.
It is possible to use a
CustomValidator control without
setting the ControlToValidate
property. This is commonly done when
you are validating multiple input
controls or validating input controls
that cannot be used with validation
controls, such as the CheckBox
control.

Related

asp.net calendar control firing validation controls

I have a set of validation controls on my asp.net page, to validate values in textfields. I also have a calendar control on the same page. When I click on the calendar image, the validation control message box pops up. How can I avoid that from appearing when the calendar icon is clicked?
Perhaps try to put the calendar control into a different ValidationGroup than the other controls.
If it is not in a validation group and the other controls aren't either, try to set a validation group for the other controls.
You can either use validation groups or if you don't want it to ever trigger validation set CausesValidation = false

Field validators in asp.net

Is it possible to have "Required Field Validator" controls to validate more than one field (example I have 12 textboxes that are required. I want to try an avoid having 12 RFV controls. If a validation does get triggered, is there a way to display a customized message ("textA is empty" or "textB is empty") etc.?
You can create a custom validator that goes through validates all the controls.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
The Required Field Validator can only validate one control at a time.
You cannot do that with a RequiredFieldValidator; you could write your own CustomValidator to do that, but the validation would be on the server side rather than on the client side.
Multiple Fields Validator - An ASP.NET Validation Control is what you need.
As mentioned by everyone else you can create your CustomValidator that can validate on the client side and on the server side. There are couple of things that you must keep in mind.
1) Make sure to expose your client script as a Web Resource. This will enable the script to be cached by the browser.
2) Use a certain attribute to target certain TextBoxes. This can be performed by giving them a certain class which will be validated in your Custom Validator control.
Hope it helps!

Custom validatior in ASP.net

I'm working on an ASP.NET/C# app.
I have 2 text boxes and I need to validate if the input text for both of them are not both null
ex:
if(string.IsNullOrEmpty(TextBox1.Text) && string.IsNullOrEmpty(TextBox2.Text) )
//FAIL!!!
else
// OK!
that is, at least one txtBox has values
I was planning on using a custom validator but it seems that the validation function only gets called when something is written on the textBox i'm using has the 'control to validate'. Now, that doesn't work for me since I want to show an error message when both text boxes are empty. Is there a way to make the validation function to be called with, for example a postback? Or is there any other better approach to this case than the custom validator?
tks
If you set the 'ControlToValidate', then the validator will not fire if that control is empty. However, for a CustomValidator you can leave that empty so it will always fire.
Because the built-in validators only analyze the state of their own controls, you are forced to go with a CustomValidator for this functionality. I don't know of any better approach that still uses ASP.NET server-side validators. As Hans said, be sure to leave ControlToValidate empty, and then do the validation as in your code sample.

More than a ASP:Wizard control

How can one have more than a Wizard control on the same page without the "RequiredFieldValidator" will affect the other Wizard control?
Set the ValidationGroup property on all validators that should be grouped together, and call the Page.Validate(String) with the name of the validation group you want to validate.
You may need to disable clientside validation for this to work properly.

How do I add response from TextBox? c#

I have created my own control which contains TextBox control as a child control. this textbox has autopostback set on. when the postback is called I dont get any text in request.querrystring.
where should I specify text entered into texbox will be send to response ? or is there any easier way of sending this information? I want to have the entered text once again in textbox after postback.
thank you
For dynamically created controls, you need to create them and add them to the control tree in the Page_Init() event; Page_Load() is too late, and Page_PreRender() is way too late.
Once in the control tree, the runtime will restore the state of the control for you after a postback. You can access the contents of the control using myTextBox.Text -- you don't need to use Request.QueryString
For a control that only contains a TextBox, things would be much easier if you used a user control rather than an INamingContainer. That way, you could just use <asp:TextBox> in your .ascx markup.
A custom control? Any posted control like this loads its data in LoadPostData method. You can override this in your custom control class, and access the value from teh control via:
var text = postDataCollection[textboxID];
The textbox within your custom control has to have an ID.
If you are talking something else, let me know the specifics.
Thanks.
(TextBox)Control.FindControl("control id")
get dynamic control's value in postback , Request.Form("control client id")

Categories

Resources