<script type="text/javascript" language="javascript">
function postBackByObject(event) {
var o = (window.event) ? window.event.srcElement : event.target;
if (o.nodeName == "INPUT" && o.type == "checkbox") {
__doPostBack("", "");
}
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.EnableClientScript = true;
TreeView1.Attributes.Add("onclick", "postBackByObject(event)");
if (!IsPostBack)
{
DataSet ds = new TaxiRadarBasicInfo.BLL.TaxiCityCode().GetRegionAllList();
TreeView treeview = new TreeView();
foreach (DataRow row in ds.Tables[0].Rows)
{
TreeNode trP = new TreeNode();
trP.Text = row["CityName"].ToString();
trP.Value = row["CityCode"].ToString();
//TreeCAdd(trP, row["CityCode"].ToString());
trP.Expanded = false;
TreeView1.Nodes.Add(trP);
TreeCAdd(trP);
}
}
}
public void TreeCAdd(TreeNode trP)
{
string CityCode = trP.Value;
DataSet ds = new TaxiRadarBasicInfo.BLL.TaxiCityCode().GetCarRegionByTerminalSN(CityCode);
foreach (DataRow row in ds.Tables[0].Rows)
{
TreeNode trC = new TreeNode();
trC.Text = row["Brand"].ToString();
trC.Value = row["TerminalSN"].ToString();
trP.ChildNodes.Add(trC);
}
}
protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
RecursiveCheckCheckBox(e.Node, e.Node.Checked, true);
TreeNode current = e.Node;
while (!current.Checked && current.Parent != null && !IsChildNodesChecked(current.Parent))
{
current.Parent.Checked = false;
current = current.Parent;
}
if (e.Node.Checked)
{
RecursiveCheckCheckBox(e.Node, e.Node.Checked, false);
}
}
private bool IsChildNodesChecked(TreeNode n)
{
bool result = false;
if (n.ChildNodes.Count == 0)
{
result = n.Checked;
}
foreach (TreeNode t in n.ChildNodes)
{
if (result |= t.Checked)
{
break;
}
}
return result;
}
private void RecursiveCheckCheckBox(TreeNode n, bool isChecked, bool topDown)
{
if (topDown)
{
foreach (TreeNode l in n.ChildNodes)
{
l.Checked = isChecked;
if (l.ChildNodes.Count != 0)
{
RecursiveCheckCheckBox(l, isChecked, topDown);
}
}
}
else
{
n.Checked = isChecked;
if (n.Parent != null)
{
RecursiveCheckCheckBox(n.Parent, isChecked, false);
}
}
}