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)
{
PopulateCategoryList();
}
}
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();
ad.Fill(ds);
ddlCategory.DataSource = ds;
ddlCategory.DataTextField = "CategoryName";
ddlCategory.DataValueField = "CategoryID";
ddlCategory.DataBind();
}
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();
ad.Fill(ds);
DropDownList ddlProducts = new DropDownList();
ddlCategory.ID = "ddlProducts";
ddlProducts.DataSource = ds;
ddlProducts.Attributes.Add("onChange", "PopSelectedProductID(this.value)");
ddlProducts.DataTextField = "ProductName";
ddlProducts.DataValueField = "ProductID";
ddlProducts.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
ddlProducts.RenderControl(htw);
return sw.ToString();
}
#region
ICallbackEventHandler Members public string GetCallbackResult()
{
return GetProductsByCategoryID(Int32.Parse(argumentValue));
}
public void RaiseCallbackEvent(string eventArgument)
{
argumentValue = eventArgument;
}
#endregion
}
And here is the HTML part of the code:
<div>
<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>
<div class="ProductsDropDownList">
<table>
<tr>
<td colspan="2">
Select a product: </td><td> <div id="MyDiv"></div>
</td>
</tr>
</table>
</div>
</div>