I have used check box in gridview . Now i want to validate those checkboxes as if no check box is selected a message shud pop up saying please select check box and thn press submit
Thanks
Smartdev
You can use a CustomValidator which validates if the user checked the Checkbox. You must set ValidateEmptyText to true. Following an example which you can rewrite easily to validate a Checkbox: CustomValidator in a GridView with a ValidationSummary control
There is a tutorial here that will walk you through how to perform validation using Javascript on the client side.
You can easily modify the logic with in the javascript to throw up an alert if no boxes are checked.
IMO client side validation would be better as it will save a trip to the server.
In addition, you could implement the AJAX Mutually Exclusive CheckBox in the GridView item template to force users to check a maximum of one box. However, from your question I'm not 100% sure if you require this.
Related
ASP.NET WebForms 4.51
Having read things like How to validate two groups of controls with 1 button and a validation group?
I have a form with the following logical inputs:
Name (TextBox)
Last Name (TextBox)
Profession (Drop Down List)
Profession has the following items:
Bus Driver
Taxi Driver
Not Applicable
Now based on the profession I display a number of other text fields. I have required field validators on all the fields 1..3 as well as the fields for the profession (4..8).
I have a single submit button which triggers the validation. So my question is what is the easiest way to validate controls: 1..3 should the selection be Not Applicable for profession and 1..8 for the other professions from a single button.
I can think of a couple of ways but they are all "messy" and I was looking for a better approach:
1) Have two required field validators per field - one when validating for Not Applicable and one for when a profession is selected. Toggle the validation group when the profession is selected for the button.
I tried this but it does not seem to work as I still get both validators firing per field.
2) Write custom client side validators that take into account what is selected in the drop down for profession.
A maintenance nightmare. I would prefer to use any built in functionality MS provides.
So has anyone got any suggestions on how to validate the same sets of controls differently based on what has been selected in another control?
You could write a simple javascript validation script. You can not validate the javascript on serverside using "page.Isvalid".
Your only option is to write a custom validator. I just don't know why that should be a maintenance nightmare. If you're using the javascript validation you should validate the values in the code behind anyway. Otherwise the user could just disable javascript and enter anything he wants.
Hope it helps...
I have a form that asks a user for their first name, last name, phone number, ID number, username, and check boxes to choose either faculty or student in that order. The first two text boxes have two validators each, a regular expression: ^[a-zA-Z- ]*$ and required field validator. After that I created some custom validators as in addition to the text box for phone number, ID number, and username I have a "Don't know?" checkbox in case they forgot. So it validates to make sure they either typed in something and if not to make sure the check box is checked. Now, if I fire up the site and simply click the confirm button only the first two fields trigger their validation, that is the first and last name fields. Now, if I put in a first and last name and click confirm button again the rest of the validators trigger as normal. How can I get them to all trigger at once? Thanks.
Your custom validator logic may be implemented only in server side. You may have to provide the validation logic at client side by writing a javascript method and specifying it in ClientValidationFunction property. The link also provides an example on how to implement it in the client side.
I am dynamically generating the form based on the drop down selected.
The form consists of fields (data entry for decimal values + few text fields). Have to add all the decimal values at the end and update the Total TextBox with that value. Total Textbox is disabled.
When I click Save button on the form after the user have entered their values, whole form is persisted in viewstate except the disabled textbox. When I enable the textbox, everything works fine. Mind you, I am dynamically generating the form and updating the value of the total textbox using javascript to calculate (adding all decimal fields).
P.S. I am doing everything right for persisting the viewstate.
So what has the enabled/disabled got bearing on the viewstate
Basically, I added two statements to get it working.
txtBox.Attributes.Add("readonly", "readonly");
txtBox.Style.Add("color","gray");
When I used txtBox.Enabled = false, it didn't persist viewstate but did it alternatively using above two statements in my code-behind page
Yes, disabled form element will not send it's value to server side, you can look request header. disabled element not appeared at "get" or "post" collection.
If you want set user can't edit it, you can set it as readonly.
Add javascript on the page:
function enableTextBoxes() {
$("input[type='text'][disabled='disabled']").removeAttr("disabled");
}
And add to server code (in Page_Load, PreRender or some else method)
ClientScript.RegisterOnSubmitStatement(typeof(Page), "enableTextBoxes", "enableTextBoxes();");
If you use UpdatePanels then utilize the ScriptManager.RegisterOnSubmitStatement method
Please create custom text box rather than using actual textbox instance.
inherit textbox in your custom textbox and add this textbox in your dynamic form.
I need to collect additional information from the user when they initiate a delete on a radgrid. Right now I am presenting the user with a confirmation dialog box that lets them click "OK" to continue but I need to add an additional textbox to collect a reason.
How do I do that?
See the documentation here: http://www.telerik.com/help/aspnet-ajax/grdconfirmationdialogs.html
Looks like you'll need to do a custom confirm dialog (possibly with a jQueryUI modal?) to add that form field in there.
Additionally, check this documentation: http://www.telerik.com/help/aspnet-ajax/grdaddingdeleteprompt.html
Specifically look at the heading "Display confirmation dialog with text including column cell value." They are talking about having the confirm show a value from the RadGrid, but you could modify that technique to do your custom confirm box.
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.