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

In most of the applications we need DropDownList dependency. This means that when one dropdownlist is selected then the child dropdownlist is populated with the data which was dependent on the first dropdownlist. ASP.NET 2.0 Callbacks makes it very easy to implement such feature. Here is the complete code for the dropdownlist dependency using ASP.NET client callbacks.

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.IO;

public partial class ClientCallbackDropDownList : System.Web.UI.Page, ICallbackEventHandler


protected string argumentValue;

protected void Page_Load(object sender, EventArgs e)


// Create the client callbacks

string cbReference = ClientScript.GetCallbackEventReference(this, "arg", "RecieveServerData", "context");

string script = "function CallServer(arg,context) {" + cbReference + "}";

ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", script, true);

if (!Page.IsPostBack)





private void PopulateCategoryList()


string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true";

SqlConnection myConnection = new SqlConnection(connectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories",myConnection);

DataSet ds = new DataSet();


ddlCategory.DataSource = ds;

ddlCategory.DataTextField = "CategoryName";

ddlCategory.DataValueField = "CategoryID";



private string GetProductsByCategoryID(int categoryID)


string connectionString = @"Server=localhost;Database=Northwind;Trusted_Connection=true";

SqlConnection myConnection = new SqlConnection(connectionString);

SqlCommand myCommand = new SqlCommand("SELECT * FROM Products WHERE CategoryID = @CategoryID", myConnection);

myCommand.Parameters.AddWithValue("@CategoryID", categoryID);

SqlDataAdapter ad = new SqlDataAdapter(myCommand);

DataSet ds = new DataSet();


DropDownList ddlProducts = new DropDownList();

ddlCategory.ID = "ddlProducts";

ddlProducts.DataSource = ds;

ddlProducts.Attributes.Add("onChange", "PopSelectedProductID(this.value)");

ddlProducts.DataTextField = "ProductName";

ddlProducts.DataValueField = "ProductID";


StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);


return sw.ToString();


#region ICallbackEventHandler Members

public string GetCallbackResult()


return GetProductsByCategoryID(Int32.Parse(argumentValue));


public void RaiseCallbackEvent(string eventArgument)


argumentValue = eventArgument;





And here is the HTML part of the code:


<h2>DropDownList Dependency Using ASP.NET 2.0 Client Callbacks</h2>

<div class="CategoryDropDownList">

Select a category:

<asp:DropDownList ID="ddlCategory" onChange="Foo(this.value)" runat="server" />


<div class="ProductsDropDownList">



<td colspan="2">

Select a product: </td><td> <div id="MyDiv"></div>






Posted on Sunday, September 3, 2006 7:24 PM | Back to top

Comments on this post: DropDownList Dependency Using Client Callbacks

# re: DropDownList Dependency Using Client Callbacks
Requesting Gravatar...
Where is PopSelectedProductID(this.value)
Left by Conrad on Jan 23, 2007 2:21 PM

# re: DropDownList Dependency Using Client Callbacks
Requesting Gravatar...
Please suggest me. When I onchange it not work.
Left by keeyapat on Mar 07, 2007 9:56 AM

Your comment:
 (will show your gravatar)

Copyright © Mohammad Azam | Powered by: | Join free