Archive for 'Blog'


How to Clear and Repopulate Your ASP.NET DropDown List with jQuery

Posted on June 4, 2009

Yes you can call and use your ASP.NET controls from jQuery! Shereen has already posted a blog on how you can access these controls JQuery Accessing the Client Generated ID of ASP.NET Controls so feel free to check it out there first.

Recently we’ve had to create a UI that has a datepicker, and the date that a user selects should determine what values get populated in an ASP.NET dropdown.

Here’s how we did it

We’ve listed below the two controls we’re using in this example: a calendar control and an asp.net drop down list.

1
2
<input id="datepicker" type="text" />
<asp:DropDownList ID="ddlDates" runat="server" Width="100px"></asp:DropDownList>

For the date picker, we used the DatePicker control from jQueryUI. Their demos and documentation are a great starting point, if you have any troubles with the calendar on your site, drop us a line.

Next, we had to add the following references:

1
2
<script src="http://myserver/js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="http://myserver/js/jquery-ui-1.7.1.custom.min.js" type="text/javascript"></script>

Then we wired it up! Good ol’ jQuery!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function PopulateDropDownDates(dateText, numDaysToAdd)
{
    var selectedDate = new Date(dateText);
 
    //clear the dropdown
    $("#<%= ddlDates.ClientID %>>option").remove();
 
    //add todays date
    $("#<%= ddlDates.ClientID %>").append($('<option></option>').val(dateText).html(dateText));
 
    //counter
    for (var i = 0; i <= numDaysToAdd; i++)
    {
        var currentDate = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate() + i);
        currentDate = currentDate.getMonth() + 1 + "/" + currentDate.getDate() + "/" + currentDate.getFullYear();
        $("#<%= ddlDates.ClientID %>").append($('<option></option>').val(currentDate).html(currentDate));
    }
}
 
$(document).ready(function() {
 
    $("#datepicker").datepicker({
            onSelect: function(dateText, inst)
            {
                PopulateDropDownDates(dateText, 14);
            }
    });

Voila! That’s it. Works beautifully!

Tags: , ,

Comments are closed.

We Use WooThemes

Do you like the look and feel of this blog? You'll definitely want to checkout WooThemes and their awesome lineup of designs available for WordPress. Contact us for more information or to get started using WordPress for your company site, we'd love to help.

WooThemes - Premium WordPress Themes Club

We are experts

Black Ninja Software was created with the idea that great software comes from great people. We are passionate about the technologies we use and continually refine our skills to better master what we do. This is what makes us Ninjas. We architect, design, and implement solutions using Microsoft Office SharePoint Server, SQL Server, and ASP.NET

We create software

If you have a business process that needs refining or automation, or you have a current project in distress that needs rescuing, we can help. Our wealth of experience will create your great idea from scratch if that's what you need ninjas to do for you. In addition to the work we do for our clients, we also have several of our own projects that are currently being developed. We use the same tools and the same skills on our projects as we do on yours.

We train

We can train you to become ninjas like us. SharePoint is ginormous. We can break down training into areas that cover your business needs either on-site or over the web. We understand that adopting new technology can often be a challenge and we can help ease the process through our professional SharePoint training services.