MVC Validator To Make Sure At Least One Checkbox Is Checked
Dec 20, 2010
I have an ASP.NET MVC 2 project in which I've created a data transfer object to receive data from a web page form. The form has two groups of checkboxes on it. I want to validate the object to make sure that at least one of the checkboxes in each group is checked.
I'm doing the validation on the server side so that a user won't be able to hack around any client-side validation. (I will add client-side validation with jQuery later; that's easy.)
My understanding is that I have to create my own custom ValidationAttribute for my data transfer object class, but I don't understand how to create and use one that can accept an arbitrary list of checkbox properties to make sure that at least one of them is true. I am guessing I will have to call the attributes like this:
[AtLeastOneCheckbox("set1check1", "set1check2", "set1check3",
ErrorMessage = "You must check at least one checkbox in set 1.")]
[AtLeastOneCheckbox("set2check1", "set2check2", "set2check3", "set2check4", "set2check5",
ErrorMessage = "You must check at least one checkbox in set 2.")]
public class MyFormDTO
{
...
}
What would the implementation of AtLeastOneCheckboxAttribute look like?
Or is there a different way that I should do this kind of validation?
With respect to my previous questionListview Checkbox checked using jquery . The code works fine and it checks for any checkbox checked in listview and returns a alert msg but now it does not check for any required field validators in the page. I the checkbox is checked it fires button click event. I need to check for validators also and after that only it must check for checkbox validation. The link for previous question is [URL] .....
I need to merge the code for required field validation check with checkbox check.
I have a GridView that I was previously populating via an ObjectDataSource, and this code was working perfectly (ie, when the checkbox was checked Checkbox.Checked = true):
I have an html checkbox that controls a list of checkboxes. I want this checkbox to display in a sorta "null" state where it is neither true nor false. <HeaderTemplate> <div style="width:90px"> Toggle: <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" /> </div> </HeaderTemplate>
- How can I uncheck a checkbox using client side code when the other checkbox is checked? - Is there a way to check server side on form submission that at least one check box is checked?
Using Asp.net Webforms how can I validate that a checkbox has been checked. (I cannot do a postback) has to be in javascript (either custom - working with the existing validation controls for other fields. or using a .net validation control)
I have a gridview with one column of checkboxes and other columns with different custom controls. What I want is that when a checkbox is checked an event is triggered which toggles the visibility of other elements in the row .
what is the code to retrieve the id associates with each checkbox and read if whether each checkbox is checked or not in the controller action(I will need to get the status in both conditions, checked or unchecked )?
In my Code Behind in test method I am trying to set the checked to true, but in the ApplicantFormBound event when i debug it is never true...any reasons why? Basically I want to set the Validator to true when checked.
I am using the AJAX ReorderList, and inside the ItemTemplate I have a label and a checkbox. Now when in firefox I am able to change the checkbox checked state, but in IE it won't allow me to do anything with it. Its weird because the checkbox becomes that blue colour like when it gets focus, but it won't put the check in it or call the postback. The ReorderList is inside an UpdatePanel, so that I can monitor when the check changes on its postback. Below is the code:
I've a list page with check box for each row.When records are selected by checking the Checkbox, it will open a pop-up window for those selected records .When the pop-up is closed, i want to retain he checked status of the checkbox in the base page, just to keep track of what i selected. But curently it is not happening.
I have created a single checkbox in the DetailsView of my Asp page. I then created VB code behind to set the value =1 if the checkbox is checked and value =0 if checkbox is not checked. I cannot get the code to work. The record will get created in my SQL database, but the checkbox field remains NULL
In SQL table, my field and datatype is: Greenfield, bit, NULL
And the VB code behind. I originally used OnCheckedChanged but that wasnt working and it was suggested of me to use DetailsView1_ItemInserting instead: (The Dropdownlists in the code are working)
Code: Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting Dim DropDownList6 As DropDownList = CType(DetailsView1.FindControl("DropDownList6"), DropDownList) Dim DropDownlist9 As DropDownList = CType(DetailsView1.FindControl("DropDownList9"), DropDownList)
I have House_info table in database that have Pay column
I have one checkbox in admin.aspx page that I bind it from database
I want if there was data in pay column checkbox.cheked=true else checkbox.cheked=false
so in behind code I wrote below code
SqlCommand _cmd = new SqlCommand("ViewSreg", _cn); _cmd.CommandType = CommandType.StoredProcedure; _cn.Open(); SqlDataReader _dr = _cmd.ExecuteReader(); while (_dr.Read())
[Code] ....
But in both condition if there was data in Pay column Or there wasn't any data in Pay column it checked checkbox. I mean in both condition it is CHKV.cheked=true
I'm developing an an asp.net 2 application in c#. This site is being built with accessibility in mind, and as such I need a to build a registration from that validates using server side scripts and not use client side validation.I need to validate a textbox (whether any text has been entered), if a check box has been checked.
Bear in mind that I can't use any clientside code such as javascript postback that is wired into the <asp:CheckBox /> control.
I am trying to retrieve a value from the row based on the user checking the checkbox but the only way it works is opposite of what I think it should be. The problem is I can only get the values if I set "If RowCheckBox.Checked = False" So I have rows returned in the gridview:
row1, record1 row2, record2 row3, record3
I want to check row1 and row3 and grab the values record1 and record3 I use:
Dim str As String Dim row As GridViewRow Dim index As Integer = dgrdSearchResults.SelectedIndex lblStatus.Text = index For Each row In dgrdSearchResults.Rows If index = -1 Then index = 0 End If Dim RowCheckBox As CheckBox = CType(dgrdSearchResults.Rows(index).FindControl("chkResults"), CheckBox) If RowCheckBox.Checked Then str = dgrdSearchResults.Rows(index).Cells(2).Text lblStatus.text = str End If index += 1 Next
With this code I basically step out even when I have rows checked. zaybe I am looking to much but I cannot get it to return the values I need unless I change "If RowCheckBox.Checked " to If RowCheckBox.Checked = "False"