Required Field Validator For RadioButton Columns Inside Gridview - asp.net

I have GridView and one columns is a RadioButton for select. How can I set a validator for columns to check that at least one RadioButton is selected?

I posted to a similar question the other day with a solution that should work for you:
How to check that at least one RadioButtonList has an item selected?
It uses a CustomValidator with a server side check to ensure at least one item is selected. You will need to substitute looking for a RadioButton instead of the RadioButtonList and maybe a few other tweeks but the code should work for what you need. It searchs for all of one type of control and checks for a specific critera. In your case look for all RadioButton controls and ensure at least one is checked.

there is no out-of-the-box way for what you are asking, you will need to use custom validator and implement the logic yourself on the client as well as on the server side; few way you can go about it, the one that comes to mind is to have the custom validator logic check at least one of the checkboxes is checked for passing IsValid, we will need more details about your structure to provide more specific solution

Related

Can I Use GridView SelectedIndexChanged Instead of RadioButtonList SelectedIndexChanged?

I have a few reasons that I need this to happen, but suffice it to say that when I use the event handler for the RadioButtonList the sender object returns as just the RadioButtonList, and I need more information from the GridView because that's how I can uniquely identify which record my RadioButtonList is tied to.
I added an "OnSelectedIndexChanged" property to my GridView but it doesn't fire/handle when I want it to. I'm assuming this has something to do with the way the GridView is designed to work.
Does anyone have any suggestions on how to do this or a better alternative?
I also thought about using a hidden field, but I can't add it to the RadioButtonList so it won't help unless it's global. The problem with it being global is that I can't set the value uniquely to the record that has it's RadioButtonList being altered.
Clear as mud? Please ask if you need any clarification.
Thanks in advance!
Just thought I would add an answer and explain how I solved my issue.
Since the scope was too narrow, I needed to add a custom field to the radiobuttonlist. I just went ahead and added the following code:
MyRadioButtonList.Attributes.Add("NecessaryField")
I added this so that everytime a grid view row was databound this code would aldo execute, ensuring that every RadioButtonList had this field.
After that it's available for use.

.net Validating a range of controls

I have the following scenario.
I have a search page which is split into two divs. In the first, a user can create query parameters using a range of drop down menus. In the second div a user can enter text to source the query parameter and narrow the search further. On the drop down selectedindex change event, a radio button is selected indicating which search is being looked at.
i.e.
div1 div2
ddl1 tbx1
ddl2 ddl4
ddl3
rad1 rad2
Based upon their selection, the user can then click a button, btnReturn, that returns the query.
So therefore, I have the following questions.
If I have a range of drop downs, in div1, what sort of validation do I need to set up so that at least one of these drop downs needs to be selected in order for a query to be selected. I cannae just put in required fields as not all of these fields are required, a minium of one of these is need to product a reasonable search.
I am assuming I can do all of this using .net validation?
I am thinking I can use Validation Groups for div1 & div2 but is it possible to assign both groups to one control i.e. btn click? Is it a case of doing the last part programmatically?
And that concludes today's essay! any help, as always, greatly appreciated.
Create a user control (or custom control) wrapping all dropdowns. Then create a Custom Validator, set its validated control to the wrapper you created. Write a javascript function to check that at least one value is selected in the dropdowns and set the ClientValidationFunction property of the custom validator to use it.
Yes.
No, this wouldn't work in your case (unless you want a postback when switching between the DIVs). Create two submit buttons, and hide them on the client depending on the selected DIV.

Does there exist a multiple select dropDownList for asp.net?

I'd like to have a DropDownList that has a CheckBox next to each item that would allow multiple to be selected. I did a little poking around but haven't found anything that will do this, does anyone know if this can be easily achieved?
I know it can be done with a listBox, but would prefer the dropdown format to save space.
Have a look at the DropDown control from the ASP.NET AJAX toolkit: It allows you to put arbitrary controls into a dropdown-like interface. I didn't try it, but I think that it should work to put a CheckBoxList in there.
I've never seen a multi-select DropDownList on any platform. How do you want it to work?
I'm sure some clever javascript could create one.

Problem with GridView, Bind() and DropDownList

I am updating an existing ASP .NET site. This site has a custom grid control class that extends the GridView control to add a few features. Many pages in the site use the built in declarative two-way binding feature that is built into the base GridView, i.e. column templates make calls to Bind() so that data can be shown and updated automatically. This works fine in most cases. However, when binding DropDownList controls there is now a problem.
Recently I had to add a feature that allows records in some tables to be marked as 'Closed', i.e. they can no longer be referenced by new records being inserted into other tables.
When editing a data grid row that has a DropDownList of child records that can be closed, the SelectedValue property might be bound to an ID that does not exist in the list. This causes an ArgumentOutOfRange exception to be thrown. I just want the DropDownList to default to no selection if the record is closed and therefore not in the list.
I'm looking for the easiest way to solve this. If possible I don't want to have to make a lot of changes to existing code.
I can programmatically set the selected index of the DropDownList in the RowDataBound event. But I can't find a way of updating the value whilst keeping the existing update functionality.
The actual question:
Is there some way to extend the DropDownList to make it ignore invalid values for the SelectedValue property? The only example I have seen so far does not work. I think that the DropDownList caches the value in case it has not yet had its DataSource property set, so overriding the SelectedValue property is not sufficient.
Alternatively, if there is a way to use the OnRowUpdating event to manually add the data to the update then that would be OK. I have tried adding values to the NewValues dictionary on the GridViewUpdateEventArgs class but it doesn't seem to work. Note that the grids are bound to lists of objects, not DataSourceControl derived controls.
Any help would be appreciated. Thanks.
If anyone's interested, I think I solved this by overriding the PerformDataBinding method and catching the ArgumentOutOfRangeException there. I suspect that the SelectedValue property might need to be overridden as well if the order in which the two properties are bound can vary.

"Action" column in a DataReader

I have an "action" column in my repeater which shows actions a user can select for an item. The column contains ASP.NET HyperLink or LinkButton controls. Some actions are based on whether a user is in a role, which I determine programatically. I'm struggling with the best way to dynamically generate this column when I populate the repeater. Now I am assigning in-line code to the Visible property of each control, but I feel that is sloppy and not very straight forward. Would I be better served using a PlaceHolder control and populating that at runtime? What kind of methods do other people use for situations such as this?
The "normal" way to apply any sort of dynamic rendering to a Template based control such as the Repeater is to handle the ItemCreated or ItemDataBound events.
In your particular case, you could check appropriate conditions within that event handler and toggle the visibility of the relevant "Action" column.
Also, see this question where Ian Quigley posted a code snippet that should serve as a good example for you. It may also help to read my own answer which shows how to use visibility toggling in inline code.

Resources