Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

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

Posted on Tuesday, April 18, 2006 11:40 PM | Back to top


Comments on this post: Populating the ATLAS CascadingDropDown through Database Table

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
Can you post the whole project somewhere? I am having difficulty with your code. How are you populating the original DDL?
Left by Matthew on Apr 25, 2006 5:53 PM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
Hi,

You can populate the DDL on the .aspx.cs page but I have coded it in the webservice.

CategoryService service = new CategoryService();
ddlCategory.DataSource = service.PopulateCategoryList();
ddlCategory.DataTextField = "CategoryName";
ddlCategory.DataValueField = "CategoryID";
ddlCategory.DataBind();

[WebMethod]
public DataSet PopulateCategoryList()
{
SqlConnection myConnection = new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=true");
SqlDataAdapter ad = new SqlDataAdapter("SELECT CategoryName,CategoryID FROM Categories", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);

return ds;
}
Left by azamsharp on Apr 25, 2006 5:59 PM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
this does not work for me what must the imports be on the webservice
Left by Nico on May 02, 2006 5:22 AM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
I can't get this example to work either. I don't understand why the author is unable\unwilling to post a full complete (out of the box)working example.

If i could rate this post i'd give it a 1\10. Useless for beginners.
Left by David on May 19, 2006 8:16 AM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
add these 2 namespaces and try

using System.Collections.Generic;
using System.Collections.Specialized;
Left by Srini on Aug 09, 2006 4:26 AM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
Hi,

I am trying to create a web part with CascadingDropDown programmatically. I can't add any aspx or ascx page in the library project and the only way to move forward is to create a cs class file inheriting from WebPart system class. Could you please post any example to use CascadingDropDown through CS code?

thanks.
Left by Nishat Kapoor on Aug 21, 2006 3:58 AM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
Why a web service? I don't want to create a web service just to populate my dropdowns.
Left by Luis on Sep 27, 2006 2:58 PM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
Hi, this works very fine, but i can i pick the selected values to insert in the database?
When i click in the button, this fire the PostBac and i loose the selected values choosen by the client...

Thanks!
Left by Marcos on Nov 02, 2006 3:58 AM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
I dont understand the function about string knownCategoryValues, string category.
Left by satria on Nov 03, 2006 5:11 AM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
Is it possible to change the ATLAS CascadingDropDown so that it works with ListBox and multiple selection, rather than just DropDownList?
Left by Boris on Dec 26, 2006 9:43 AM

# re: Populating the ATLAS CascadingDropDown through Database Table
Requesting Gravatar...
pure crap, i agree with David
Left by Mark on Oct 01, 2007 6:34 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net