前台:
// 点击复选框时触发事件
function postBackByObject()
{
var o = window.event.srcElement;
if (o.tagName == "INPUT" && o.type == "checkbox")
{
__doPostBack("","");
}
}
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//构建TreeView
TreeNode Node00 = new TreeNode("00", "00");
TreeNode Node01 = new TreeNode("01", "01");
TreeNode Node0101 = new TreeNode("0101", "0101");
TreeNode Node0102 = new TreeNode("0102", "0102");
TreeNode Node02 = new TreeNode("02", "02");
Node01.ChildNodes.Add(Node0101);
Node01.ChildNodes.Add(Node0102);
Node00.ChildNodes.Add(Node01);
Node00.ChildNodes.Add(Node02);
TreeView1.Nodes.Add(Node00);
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;//显示CheckBox
TreeView1.ExpandDepth = 0;
}
TreeView1.Attributes.Add("onclick", "postBackByObject()");
}
protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
SetChildChecked(e.Node);
}
private void SetChildChecked(TreeNode parentNode)
{
foreach (TreeNode node in parentNode.ChildNodes)
{
node.Checked = parentNode.Checked;
if (node.ChildNodes.Count > 0)
{
SetChildChecked(node);
}
}
}
这里客户端有个 __doPostBack函数,弄清它的原理,如下:
__doPostBack(id, eventArgument)来和控件交互。doPostBack的用法是,id: 服务器控件的ClientID,eventArgument: 传回服务器的参数。这个参数在服务器端通过IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)来读取
了解到此