List:
Achieve linkage between categories and sub-categories
Private void BindIncidentCategory ()
{
Using (SPSite site = new SPSite (rootsiteurl ))
{
Using (SPWeb web = site. OpenWeb ())
{
SPList list = web. Lists ["IncidentCategory"];
SPListItemCollection listitem = list. Items;
DataTable dt = listitem. GetDataTable ();
DataView dvSort = new DataView (dt );
String [] colName = new string [] {"CategoryName "};
DataTable dtLargeSort = dvSort. ToTable (true, colName );
DropDownListIncidentCategory. DataValueField = "CategoryName ";
DropDownListIncidentCategory. DataValueField = "CategoryName ";
DropDownListIncidentCategory. DataSource = dtLargeSort;
DropDownListIncidentCategory. DataBind ();
BindIncidentSubCategory (DropDownListIncidentCategory. SelectedItem. Text );
}
}
}
Private void BindIncidentSubCategory (string IncidentCategory)
{
Using (SPSite site = new SPSite (rootsiteurl ))
{
Using (SPWeb web = site. OpenWeb ())
{
SPList list = web. Lists ["IncidentCategory"];
SPQuery query = new SPQuery ();
Query. query = "<Where> <Eq> <FieldRef Name = 'categoryname'/> <Value Type = 'text'>" + IncidentCategory + "</Value> </Eq> </Where> ";
SPListItemCollection listitem = list. GetItems (query );
DataTable dt = listitem. GetDataTable ();
DropDownListIncidentSubCategory. DataTextField = "SubCategoryName ";
DropDownListIncidentSubCategory. DataValueField = "SubCategoryName ";
DropDownListIncidentSubCategory. DataSource = dt;
DropDownListIncidentSubCategory. DataBind ();
}
}
}
Protected void DropDownListIncidentCategory_SelectedIndexChanged (object sender, EventArgs e)
{
BindIncidentSubCategory (DropDownListIncidentCategory. SelectedItem. Text. Trim ());
}
Set the AutoPostBack attribute in DropDownListIncidentCategory to true.