I have been searching for a couple of hours to try to fix this.
I am using the Select2 jquery plugin to get a dropdown with multiple select.
The problem is that the first option is always selected.
I have tried to use the tag multiple="multiple" but ASP.NET doesn't know what is that, only allow me to make it multiple="true" or multiple="false"
Any suggestion??
By default Select2 should not be preselecting any value, so my guess is that you are setting the selected value somewhere, probably in the c# code where you are populating the drop down with values. So try to clear the selected index in the Page_Load method
slEmployeeRole.SelectedIndex = -1 or in the js document ready part of the page, try clearing the drop down just to be sure that nothing is selected. And use multiple="true". $('#slEmployeeRole').val(null).trigger('change');
Related
One of my websites I am working on, http://www.michaelgworkman.com, is one that I created to help me get a job, and also help my career in general.
In have been trying to add a lightweight JQuery form validation for the contact form on this website, which is hosted on Microsoft Azure Cloud Platform and having problems. Referencing the drop down list for INTEREST in the form is always returning FALSE form when an option is not selected, and also returning FALSE when I select an item in the drop down list. Here is what the form looks like:
I have this in my form in the Razor view .cshtml file for my form:
And this is the HTML generated from the form:
When I submit the form without selecting an interest in the INTEREST drop down, it correctly returns a false from the JQuery in the file.
When I submit the form after selecting an interest, the JQuery still says an option has not been selected in the drop down list, this is how the form looks after selecting a value:
Here is the JQuery code that is always returning false even after selecting a value in the INTEREST drop down list.
I would like to know how to fix this problem, it has been stumping me for about an hour so far.
Ok I found the problem here, I was using a wrong ID for the drop down list in the form, I was trying to use $("#ContractCategory"), but that is not the correct ID, the Razor code is automatically creating the ID as id="ContractCategories_ID", based on the name of the database table being used by the Entity Framework in this ASP.NET MVC Web Application. Simple fix, a little surprised I was working on this for about an hour, now the form works correctly.
For one thing, you don't have an element with id="ContactCategory" in the markup you're showing us, so if that selector isn't finding anything then false may indeed be the correct result. Try:
alert($('#ContactCategories_ID option').is(':selected'));
But even then, since that selector finds multiple elements then the semantics of what you're doing still may not make much sense and could return unexpected results. Instead of looking for a selected option, just check the value of the form element itself. Nomrally to get the value you might do something like:
let category = $('#ContactCategories_ID').val();
And check that value:
alert(category === '');
I am looking for a textbox in C# which should work like dropdown search values when I type anything in the textbox. Those values will come from calling a method.. and I Should be able to select any of the values.
Do i need to use the asp dropdownlist or is there any other sample code available which could help me.
You should look into jQuery autocomplete feature.
http://jqueryui.com/autocomplete/
The ajax control toolkit provides Autocomplete extender to do just that. The control calls a method, which you specify, asynchronously and get all the values to be shown as an option. I would imagine these values would be retrieved when the page loads and will not be session specific so can be stored in a cache for faster retrieval.
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
How can I create a dynamic drop down list without using AutoPostBack. I ask because I just want to change the value of what the second drop down list displays and then the user clicks a button and it submits. But If I use AutoPostBack then it postbacks to page and runs code that shouldn't be run until that final box has been selected. (I use Postback in the other part of my program so using !IsPostBack isnt a option.) I looked at Javascript but ASP generates all of its controls names at runtime. What can I do? I have looked at the Ajax CascadingDropDown control but my problem with that is it contained in a XML file or a Database, I need this to be contained inside my Page. Any Ideas?
You can use the CascadingDropDown control from the AJAX Control Toolkit
Maybe this example will help? It's part of the ASP.NET AJAX Control Toolkit available here.
You can use AJAX to get the values for the second drop down list, based on the selected value of the first. Add a onchange event handler on the client-side to the first drop down list that makes the AJAX call and fills the second on success.
I have a listview in wpf and i am swapping two items index..
the swapping must be visible to the user.
i tried giving thread delay..
it didnt work
How to do that..
If I was going to do this I would delete the list view and lock it up where you can't use it again. Then code whatever your list view was outputting in C# using whatever you use to query your database(I think LINQ to SQL is the most robust solution right now) and then use a string builder to construct the html. This way you can assign a id to each div and append an incrementing number to the end of the id. Finally you could write your javascript and use the id's. Here is a link that shows how to build a gridview without using a gridview control.
See the first answer to the question in this link: How to show pop up menu from database in gridview on each gridview row items?
I am not sure whether it will work for your problem or not.
I think you need some kind of animation there. If it is web project, you can use jQuery animation to do that.
How can use a dropdown list without the autopostback=true.
The value on the server is not being changed according to the one selected from the client side. As I already stated I do not wish that for each dropdown I have the autopostback will trigger a post back.
Any time I have lost the value of the drop-down it is because I messed up and repopulated the drop down before handling the value change. For me, it has been drop-downs that I need to do something special with like add item attributes for Javascript, etc. This is data that needs to be added on every page load (aka data that is not persisted in the drop down like the names and values of each item). In these cases I have done this work on load, then I try to retrieve the value later in the page lifecycle and DOH!
Here is the page lifecycle:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Dollars to donuts that is what is happening. You are probably just reloading the items before you get to handling whatever postback event you are using to grab the value. If you are doing this and cannot get around this work flow, just save the selected index at the beginning of the logic that populates the drop-down, then set the selected index of the drop down with that value when done.
it'll be saved in the viewstate, so the value will be correct when you do eventually post back, and if you're really desperate to get the current value without a postback, javascript would be the way to do this.
Worst case you can grab the value right off the request object:
string selectedID = Request[DropdownControl.UniqueID];
You should make sure you are only filling the select box with options during the initial page load, and not again during the postback
if (!this.Page.IsPostBack) {
//fill select box here
}