.net Validating a range of controls - asp.net

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.

Related

How to generate a dropdown list from another?

I want to generate a drop down list from another drop down list. That is I have a dropdown of countries. When selecting a country,another dropdown must come with values as states of that specific country. How to do that in asp.net using c#?
for each country you have, add a new list item to the drop down list, with text the country and the value some id of the country. On the second drop down list, set the auto post back property to true and add an event to the on selected item change. In the event code, get the selected item and by the second ddl.
Try it!
Tip: add a hidden field on the page, and on the selected item changed event from the first ddl, set the value of the hidden field, the selected value. On the page_load event, verify if the value is string.empty and if is an id in the value. If it is, bind the second ddl.
The technology you're looking for is called a Cascading Dropdown.
If you are using WebForms then Ajax Control Toolkit then this has one built in:
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
Otherwise you might need to do a further search if you are using MVC.
PS I'm not particularly proud of posting a link to the Ajax Control Toolkit as its not the best library out there but it is an easy drop in for what you want. If you're serious about doing a good job then I'd search for better options for cascading dropdowns.

Required Field Validator For RadioButton Columns Inside Gridview

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

Asp.NET AJAX control update field on form

I have a form with several text fields, a couple of drop down lists, and a custom asp.net control.
The requirement I have is that when the values of certain fields ( some are in the main form, some are inside the control ) the user will be alerted that the settings will not take place unless they also restart the processing and will have the option to reset it. If they exercise this option by pressing on the restart button, we execute an additional restart() method call on the server side on top of everything else.
In order to create this alert system I need to know when these required fields have changed. For the fields in the main form/page I store the original values in hidden fields when the page gets created (possible because the page is static). Then when the submit button is pressed I check the current values against the original values stored. If any of them differ I will alert the user.
For the fields in the custom control, I created a boolean property that indicates whether changes in the fields of interest took place. This control is highly dynamic contains a variable number of lists, some of them with over 100 items that can be selected or deselected. So this boolean property is able to identify if the changes I am looking for took place.
The problem is I need to get the value of this property when the user clicks on Submit, otherwise it will not contain the right value. But at the same time I want to avoid a postback. I do not want the whole page to reload. I want to get that value asynchronously somehow.
Since the whole page is stateless the question is how do I accomplish this? How do I accomplish the call to the property? And after I make the call and I get the result where do I store it so it's accessible from javascript code on the client side.
You can try putting a timer control in your ajax panel. Then on a set interval, you will evaluate the boolean property in your code behind.
Alternatively you can check the hidden field "changed" event using JavaScript or jQuery
http://api.jquery.com/change/

jquery: populating a select list from an autocomplete?

The basic problem: selecting a few items from a list of thousands.
The potential solution:
I have an autocomplete field that searches the db, and returns a name/id pair. This is working fine.
The next step is to preserve the selected IDs, and allow the user to remove some if needed. For this, I've been looking at using a select, and was hoping a UI something like that provided by this, but it doesn't work: it allows you to select items that already exist in the select, but doesn't work with a dynamically created select.
The final step is a traditional postback (using a submit button, this is in asp.net webforms) where I'll need to have access to the final list of IDs.
Are there other options for this?
Based on your comments below, there are a lot of ways to skin this cat. The following approach is similar to the SelectList idea only it doesn't use a dropdown list. The nice thing about the Listbox versus the DropdownList is that the user will be able to view many items at once. Of course, the choice of using a Listbox or a DropdownList doesn't really matter as they both essentially provide the same functionality. The key about this answer is that values are stored on the client until you're ready to submit.
Create an autocomplete textbox that dynamically fills a Listbox as you type.
Clicking on a Listbox item results in two things happeing:
The ID of the selected item is stored in a client-side array
A list of items are rendered/re-rendered on the page exactly like the SelectList. Clicking the red 'X' will remove the ID from the array and re-render the list. You'll have to do a bit of jQuery coding on your side but it isn't much.
The above steps are repeated until the user has selected all of their items.
Upon clicking "Save", only the selected items are submitted to the server for processsing.

jQuery UI autocomplete for multiple key/value selections in ASP.NET

How do I handle selection of multiple items with autocomplete? The objects I return from my JSON web service contain an ID and a Label - the ID is the ID of the entity in the database, and the Label is some text to display for the user.
At the moment, when I select an item in the autocomplete dropdown, the value of the item's ID is stored in a hidden field, and the label is displayed. When I remove the label, I clear the ID of the hidden field - this is done by adding an anchor element to the DOM that handles this.
Now, I want to have multiple selections. I want to be able to enter some text, get an autocomplete dropdown, select an item and some other options, then be able to click an 'Add New' button or the likes to be able to select another instance.
For example, I'd type in a person's name and get an autocomplete selection. I'd select a person, and then enter their age, and click 'Add'. The person's id, name, and age will be stored somewhere so that I can retrieve it on the server side when I post back.
I'm not quite sure how to do it? I'm thinking of a hidden field - I assume that many hidden fields of the same name/id turn up on the server side as an array, which I can then use. But I haven't tried this yet in ASP.NET.
How have you gone about this problem?
Well, no suggestions. I did it eventually by, in the autocomplete selection, creating a nicely styled span. It's text contains the label, it contains an anchor element that I bind a click event to that removes the element if necessary, and I use the jQuery data API to store the ID value on the span. This span gets added to a container div, before the textbox that I'm using to autocomplete. I also bind a keyup event to the textbox to check for backspace - when it's pressed and the textfield is empty, then I remove the last autocomplete item. It's a little more complex though, since by the time the keyup event is pressed, the character that was being removed from the textbox has already been removed, so I store the actual value in the keydown event too using the jQuery API and check this in the keyup event.
There is a hidden field on the form, that I keep populated with a comma-separated list of ID values extracted from the span's data. This can be kept in sync when adding/removing items, or only when I post back - it's simplest to just clear it and repopulate it, and is quite efficient as far as I can tell.
When loading the page, the spans and hidden field must be generated.
Hope this helps anyone else looking for a solution.

Resources