Thanks for your response D - Premier Data,
OK. Here is a simple demo page which has two Lists on the webform
displaying the States and Counties. When user select a certain State, the
page will be posted back and the County Listbox will displaying the
counties of that selected state. I've also using the ASP.NET's Application
Cache to cache the DataTable( state and county). YOu can have a look if you
have interests.
Hope also helps :--)
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
===========aspx================
<HTML>
<HEAD>
<title>state_county</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="80%">
<tr>
<td width="80">
Current State:
</td>
<td>
<asp

ropDownList id="lstState" runat="server"
AutoPostBack="True"></asp

ropDownList>
</td>
</tr>
<tr>
<td>Current County:</td>
<td><asp:ListBox id="lstCounty" runat="server"
Width="226px"></asp:ListBox></td>
</tr>
</table>
</form>
</body>
</HTML>
========code behind============
public class state_county : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList lstState;
protected System.Web.UI.WebControls.ListBox lstCounty;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindStateList();
BindCountyList();
}
}
private void BindStateList()
{
if(Cache["STATE_COUNTY"] == null)
{
CacheStaticData();
}
DataSet ds = Cache["STATE_COUNTY"] as DataSet;
DataTable dtState = ds.Tables["State"];
lstState.DataSource = dtState;
lstState.DataTextField = lstState.DataValueField = "Name";
lstState.DataBind();
lstState.SelectedIndex = 0;
}
private void BindCountyList()
{
string currentstate = lstState.SelectedValue;
if(Cache["STATE_COUNTY"] == null)
{
CacheStaticData();
}
DataSet ds = Cache["STATE_COUNTY"] as DataSet;
DataTable dtCounty = ds.Tables["County"];
DataView dv = new DataView(dtCounty, "State='" + currentstate +
"'",null, DataViewRowState.CurrentRows);
lstCounty.DataSource = dv;
lstCounty.DataTextField = lstState.DataValueField = "Name";
lstCounty.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.lstState.SelectedIndexChanged += new
System.EventHandler(this.lstState_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region ---Helper Functions---
private void CacheStaticData()
{
System.Random rand = new Random((int)DateTime.Now.Ticks);
DataSet ds = new DataSet("ds");
DataTable dtState = new DataTable("State");
DataTable dtCounty = new DataTable("County");
dtState.Columns.Add("Name");
dtCounty.Columns.Add("Name");
dtCounty.Columns.Add("State");
ds.Tables.Add(dtState);
ds.Tables.Add(dtCounty);
int i = 0,j=0;
for(i = 0; i<8; i++)
{
DataRow drS = dtState.NewRow();
drS[0] = "State_" + i;
for(j=0;j< rand.Next(5,10); j++)
{
DataRow drC = dtCounty.NewRow();
drC[0] = "County_" + j + "_" +i;
drC[1] = drS[0];
dtCounty.Rows.Add(drC);
}
dtState.Rows.Add(drS);
}
Cache.Add("STATE_COUNTY", ds, null, DateTime.MaxValue,
TimeSpan.FromHours(12), System.Web.Caching.CacheItemPriority.Normal, null);
}
#endregion
private void lstState_SelectedIndexChanged(object sender,
System.EventArgs e)
{
BindCountyList();
}
}