Geeks With Blogs

@azamsharp
  • azamsharp The WWDC app says that there are new videos but there is nothing under videos!!! about 500 days ago
  • azamsharp Can I post my opinion on iOS 7 Beta or is it under NDA or something? about 500 days ago
  • azamsharp iOS 7 BETA installed successfully! about 500 days ago
  • azamsharp iOS 7 BETA installed! Now restoring! I am on NET10 which uses AT&T network. Hopefully 3G will still work! Lets c.. about 500 days ago
  • azamsharp The missing of button borders in Xcode 5 makes it hard to determine the clickable area. about 500 days ago
  • azamsharp I might wait for iOS to be released before I can put it on my device! Don't want a bricked or unstable device! about 500 days ago
  • azamsharp Android has many features that no one uses, number of versions that no one upgrades to and thousands of apps that no one pays for. about 501 days ago
  • azamsharp Maybe it is just me but the new iOS 7 UI looks like Windows Phone 8 UI! #maybeIamCrazy about 501 days ago
  • azamsharp Anyone using H20 network upgraded to iOS 7 BETA Keep me updated if you face any problems. about 501 days ago
  • azamsharp @merowing_ You already downloaded it! I cannot even load the developer's website! :( about 501 days ago

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)

{

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>

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: GeeksWithBlogs.net | Join free