Populating the ATLAS CascadingDropDown through Database Table

CascadingDropDown is simply great, it allows you to create the dependent DropDownLists with no postback (NO POSTBACK is the prime purpose of the ATLAS framework). You can configure your CascadingDropDown to be populated from the database table rather then the XML file.

Here is the HTML part of the code:

 <atlas:ScriptManager ID="MyScriptManager"
 runat="server"></atlas:ScriptManager>
    
    Category: <asp:DropDownList ID="ddlCategory"
    runat="server"></asp:DropDownList> <br />
    Products: <asp:DropDownList ID="ddlProduct"
    runat="server"></asp:DropDownList>
   
            <div>
            <cc1:CascadingDropDown ID="catCDD"
            runat="server">
            <cc1:CascadingDropDownProperties
            ParentControlID="ddlCategory" Category="CategoryName"
            TargetControlID="ddlProduct" PromptText="Please select
            a category" ServiceMethod="GetProductsByCategoryID"
            ServicePath="CategoryService.asmx" />
            </cc1:CascadingDropDown>

And here is the WebService that contacts the database table and gets the database based on the selection. I am also using Data Caching in the example below just to increase performance.

[WebMethod]
    
public CascadingDropDownNameValue[] GetProductsByCategoryID
    (
string knownCategoryValues, string category)
    {
        
// Cache the results for performance 
               

        
string[] categoryValues = knownCategoryValues.Split(':',';');
        
int categoryID = Int32.Parse(categoryValues[1]);

        
string key = "Category" + categoryID;

        DataSet ds = (DataSet)HttpContext.Current.Cache[key]; 

        
if (null == ds)
        {

        ds = 
new DataSet(); 
            
            
// We need to get the data from the database
        
string query = "SELECT ProductID,ProductName FROM Products
        WHERE CategoryID = @CategoryID";
        SqlConnection myConnection = 
new SqlConnection
        ("Server=localhost;Database=Northwind;Trusted_Connection=true");
        SqlCommand myCommand = 
new SqlCommand(query, myConnection);
        myCommand.Parameters.AddWithValue("@CategoryID", categoryID); 
        SqlDataAdapter ad = 
new SqlDataAdapter(myCommand);        
        ad.Fill(ds); 
 
            
// Put the item in the cache 
        
HttpContext.Current.Cache.Insert(key, ds); 

        }

        
else
        
{
            ds = (DataSet) HttpContext.Current.Cache[key]; 
        }
           
       
        List<CascadingDropDownNameValue> values =
        
new List<CascadingDropDownNameValue>();

        
foreach (DataRow row in ds.Tables[0].Rows)
        {
            values.Add(
new CascadingDropDownNameValue((string)
            row["ProductName"],row["ProductID"].ToString()));
        }

        
return values.ToArray();     
    }

 

powered by IMHO 1.3

Print | posted @ Tuesday, April 18, 2006 11:40 PM

Twitter