AJAX Control ToolKit Tutorial CascadingDropDown Using ASP.NET C#



AJAX Control ToolKit Tutorial CascadingDropDown Using ASP.NET C#

 


Looking for a Tutorial Based on the AJAX control CascadingDropDown Extender?
Have you looked for a car recently? Visited a used car website lately? noticed how they had a dropdown menu?
Well they were using a CascadingDropDown or a variation of it.
The CascadingDropDown Extender is a Drop down menu where you can categorize your data, and by using a Web Service Method, you can avoid PostBack and have a much smoother transition between DropDowns. In Today’s example were going to utilize this tool like you would in a Used Car Website,
We’re going to use an XML file with Data in it, you can build your own Data, but we suggest you download the Source file as this is a bit of a tough Extender to use and it can get confusing quite quickly.
The DropDown Works as if it was a little server, To render it shortly for those who do not know CascadingDropDown is an extender control used to fill the data in one DropDownList depending on the value chosen in another DropDownList. It gets all the necessary data through AJAX using a web method.


Step 1. Let’s Begin the Project
1.       We can begin by ppening a new Website in Visual Studio 2010.
Then we can add a new webform we will name is CascadingDropDown.aspx.
2.       Then we will want to add a web service named CarsService.asmx. The web service is what we will store the data for the DropDownlist on our “little server” like we mentioned above.
3.       Now let’s move to Design View in Visual Studio and Let’s Add a DropDownList and CascadingDropDown to the DropDownList.
4.       After that we can set a label just to show what you which vehicle you selected.
5.       Now here is the important part. You must set the Target Control Properly if you are doing this on your own. , Remember to Set the Service Path and Service Method and Parent Control. You must either define an XML Source or a Database Source to display the dropdown data. To See the Rest of the Web Service Method Download the Rest of the source file
 
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
 
 
using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Services;
using System.Xml;
 
///<summary>
/// Helper web service for CascadingDropDown sample
///</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class CarsService : WebService
{
    // Member variables
    private static XmlDocument _document;
    private static Regex _inputValidationRegex;
    private static object _lock = new object();
 
    // we make these public statics just so we can call them from externally for the
    // page method call
    public static XmlDocument Document
    {
        get
        {
            lock (_lock)
            {
                if (_document == null)
                {
                    // Read XML data from disk
                    _document = new XmlDocument();
                    _document.Load(HttpContext.Current.Server.MapPath("~/App_Data/CarsService.xml"));
                }
            }
            return _document;
        }
    }
 
    public static string[] Hierarchy
    {
        get { return new string[] { "make", "model" }; }
    }
 
    public static Regex InputValidationRegex
    {
        get
        {
            lock (_lock)
            {
                if (null == _inputValidationRegex)
                {
                    _inputValidationRegex = new Regex("^[0-9a-zA-Z \\(\\)]*$");
                }
            }
            return _inputValidationRegex;
        }
    }
 
    ///<summary>
    /// Helper web service method
    ///</summary>
    ///<param name="knownCategoryValues">private storage format string</param>
    ///<param name="category">category of DropDownList to populate</param>
    ///<returns>list of content items</returns>
    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
    {
        // Get a dictionary of known category/value pairs
        StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
 
        // Perform a simple query against the data document
        return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category, InputValidationRegex);
    }
}
 
 
The markup of the page will look like
 
 
<script runat="server">
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Get selected values
        string make = DropDownList1.SelectedItem.Text;
        string model = DropDownList2.SelectedItem.Text;
        string color = DropDownList3.SelectedItem.Text;
 
        // Output result string based on which values are specified
        if (string.IsNullOrEmpty(make))
        {
            Label1.Text = "Please select a make.";
        }
        else if (string.IsNullOrEmpty(model))
        {
            Label1.Text = "Please select a model.";
        }
        else if (string.IsNullOrEmpty(color))
        {
            Label1.Text = "Please select a color.";
        }
        else
        {
            Label1.Text = string.Format("You have chosen a {0} {1} {2}. Nice car!", color, make, model);
        }
    }
 
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
    {
        return new CarsService().GetDropDownContents(knownCategoryValues, category);
    }
</script>
 
 
        <br />
       
        <asp:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
            Category="Make" PromptText="Please select a make" LoadingText="[Loading makes...]"
            ServicePath="~/CarsService.asmx" ServiceMethod="GetDropDownContents" />
        <asp:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
            Category="Model" PromptText="Please select a model" LoadingText="[Loading models...]"
            ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
        <asp:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
            Category="Color" PromptText="Please select a color" LoadingText="[Loading colors...]"
            ServicePath="~/CarsService.asmx" ServiceMethod="GetDropDownContents"
            ParentControlID="DropDownList2" />
    
                <asp:Label ID="Label1" runat="server" Text="[No response provided yet]" />
    
 
 
 


 
Conclusion
In Today’s example we showed you how a CascadingDropwdown works and wow to manipulate it using Web Service Methods. 
Now aternatively a Database can be used as well, and we will also be doing a tutorial on how to set up a CascadingDropDown Extender with a Database File
This CascadingDropdown can be used in a variety of ways to enhance your end users online experience by allowing them to narrow down by brand, type and or weight or whatever you can think of. This tool can seem a little confusing at first, but to avoid any confusion we recommend you to download the Source File to follow along.
See how easy that was?
Not as Easy When we,
 
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect'shelp, we were able to avoid any headaches!
Thank you for reading our tutorial on AJAX Controls, we aim to provide the best AJAX tutorials. Learning AJAX in ASP.NET is easy and we hope you are getting your hands dirty by trying different things and coming up with your own ideas. If you have any questions or concerns, please feel free to suggest a tutorial or you can comment below in the comments section.
 
AJAXTutorials.com
 


Comments
Sengkang West Way EC said:

Several buses are available near Lush Acres EC along with shopping centers and restaurants. Lush Acres EC is also near Waterway Point, the shopping, dining and entertainment hub which is scheduled to open in 2 years time in Sengkang. Also, it is right beside Fernvale Link. Entertainment for your loved ones and friends are therefore at your fingertips with the full condo facilities as well as the amenities near Lush Acres EC.

Posted Jun 21, 2013 12:46 AM
Former Tai Keng Court said:

Future residents will be able to walk to the existing Bartley MRT in the Circle Line. With such a short drive to the city area as well as the orchard and bugis area, entertainment for your love ones and family will come at a stone’s throw away.

Posted Jul 7, 2013 5:45 AM
StoneBaker said:

Again super post. I have added this site in my favorite list. It is helping me in building a website.

Posted Jul 24, 2013 1:26 PM
Sea Horizon said:

Sea Horizon EC facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Pasir Ris.

Posted Jul 28, 2013 9:48 PM
Skypark Residences said:

Future residents will be able to walk to the nearby Sun Plaza or a short drive to Causeway Point for some family fun and gatherings. A truly unique lifestyle awaits you at Skypark Residences.

Posted Aug 4, 2013 11:11 PM
hanna ph metre said:

I always wanted to be able to fill a panel with information gathered from all around the page directly

Posted Aug 7, 2013 2:15 PM
perücken said:

Verschiedene Arten von Perücken geben Sie ein anderes Bild

Posted Aug 19, 2013 2:03 AM
Matt Considine said:

This is an awesome page. It consists of a lot of vital content and articles using this question. data basing the software right into any readable and additionally well crafted content

Posted Aug 24, 2013 1:58 AM
The Panorama said:

With such a short drive to the city area as well as the orchard and bugis area, entertainment for your love ones and family will come at a stone’s throw away in The Panorama.

Posted Aug 30, 2013 1:04 AM
The Inflora said:

The Inflora Condo has full and unique facilities, which includes a guard house, clubhouse, children’s playground, swimming pool, kid’s pool, function room, dining pavilion, playground, poolside BBQ, waterjet pool.

Posted Sep 13, 2013 2:30 AM
Turun Berat Badan said:

Hello there nice studying your informative articles. I found your blog and simply wished to say thanks!! It's assist me an awesome great deal

Posted Sep 14, 2013 10:56 AM
Sky Vue Condo said:

Sky Vue Condo facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Bishan.

Posted Sep 17, 2013 12:53 AM
Lycium Barbarum said:

I was exploring for this Lycium Barbarum for weight loss management; when I chanced upon this particular AJAX Control ToolKit Tutorial CascadingDropDown Using ASP.NET C# posted on your blog site. I did adore it and even I am intending in dropping in in the future.

Posted Sep 20, 2013 5:09 AM

Leave a Comment