前台:
<script language="javascript">
function postBackByObject()
{
var element = window.event.srcElement;
if (element.tagName == "INPUT" && element.type == "checkbox")
{
var checkedState = element.checked;
while (element.tagName != "TABLE")
element = element.parentElement;
UnCheck(element);
element = element.nextSibling;
if (element == null)
return;
var childTables = element.getElementsByTagName("TABLE");
for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++)
Check(childTables[tableIndex], checkedState);
}
}
function UnCheck(table)
{
if (table == null || table.rows[0].cells.length == 2) // This is the root
return;
var parentTable = table.parentElement.previousSibling;
Check(parentTable, false);
UnCheck(parentTable);
}
function Check(table, checked)
{
var checkboxIndex = table.rows[0].cells.length - 1;
var cell = table.rows[0].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == 1)
checkboxes[0].checked = checked;
}
</script>
后台:
this._offerCounties.Attributes.Add("onclick", "postBackByObject()");
public void BindCountriesTreeView()
{
_offerCounties.Nodes.Clear();
List<OfferCountry> allCountries = BLLOffer.GetAllCountries();
DataTable dtCounties = ConvertHelper.ToDataTable<OfferCountry>(allCountries, "Id", "Name", "ParentId");
intiTree(_offerCounties.Nodes, "-2", dtCounties);
List<OfferCountry> offerCountries = BLLOffer.GetOfferCounties(_offerId.Value.ToString());
setOfferCountries(this._offerCounties.Nodes, offerCountries);
}
/// <summary>
/// Bind the treeview
/// </summary>
/// <param name="Nds"></param>
/// <param name="parentId"></param>
/// <param name="dataSource"></param>
private void intiTree(TreeNodeCollection Nds, string parentId, DataTable dataSource)
{
DataView dv = new DataView();
TreeNode tmpNd;
string strId;
dv.Table = dataSource;
dv.RowFilter = "ParentId='" + parentId + "'";
foreach (DataRowView objRow in dv)
{
tmpNd = new TreeNode();
strId = objRow["Id"].ToString();
tmpNd.Value = strId.ToString();
tmpNd.Text = objRow["Name"].ToString();
tmpNd.ShowCheckBox = true;
Nds.Add(tmpNd);
intiTree(Nds[Nds.Count - 1].ChildNodes, strId, dataSource);
}
}
/// <summary>
/// Recursive the tree and get the countries checked.
/// </summary>
/// <param name="nodes"></param>
/// <param name="checkedCountry"></param>
private void RecursiveTreeView(TreeNodeCollection nodes, ref List<OfferCountry> checkedCountry)
{
foreach (TreeNode node in nodes)
{
if (node.Checked)
{
if (node.ChildNodes.Count == 0)
{
OfferCountry offerCountry = new OfferCountry();
offerCountry.Id = node.Value.ToString();
offerCountry.OfferId = _offerId.Value.ToString();
checkedCountry.Add(offerCountry);
}
}
RecursiveTreeView(node.ChildNodes, ref checkedCountry);
}
}
浙公网安备 33010602011771号