有的时候,需要在点击树节点的复选框时,触发事件来提交到后台,如是写了下面一段代码:
.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="test.aspx.cs" Inherits="Infotech.MES.Web.MainPageReport.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>树复选框的点击事件</title>
<link href="../Css/Skins/Css.aspx" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function refireshMenus(){
window.top.frames["leftFrame"].location.reload();
}
// 点击复选框时触发事件
function postBackByObject()
{
var o = window.event.srcElement;
if (o.tagName == "INPUT" && o.type == "checkbox")
{
//这里的第一个参数是UpdatePanel ID,因为我使用了MS的ASPAJAX来实现局部刷新
//如果没有使用MS的ASPAJAX,这里的两个参数都可以为空
__doPostBack("UpdatePanelManager","");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table border="0" cellspacing="0" style="width: 100%; overflow: hidden; table-layout: fixed;"
id="table" class="contentBackGroundColorStyles">
<tr>
<td valign="top" id="tdRight" class="rightContentBorderStyles">
<table style="font-size: 12px; border-collapse: collapse" cellspacing="0" cellpadding="0"
width="100%" border="1">
<tbody>
<tr style="height: 20px">
<td width="45%">
待选</td>
<td width="10%">
</td>
<td width="45%">
已选</td>
</tr>
<tr style="height: 540px">
<td class="leftContentBorderStyles" valign="top" width="45%">
<!--待选树-->
<div style="overflow: auto; width: 100%; height: 550px" id="DivSelected">
<asp:UpdatePanel ID="UpdatePanelManager" runat="server">
<ContentTemplate>
<asp:TreeView ID="TreeViewUserRight" runat="server" ShowLines="True" ExpandDepth="0"
CssClass="treeViewStyles" EnableClientScript="False" OnTreeNodeCheckChanged="TreeViewUserRight_TreeNodeCheckChanged"
ShowCheckBoxes="Leaf">
<ParentNodeStyle Font-Bold="False"></ParentNodeStyle>
<SelectedNodeStyle HorizontalPadding="0px" CssClass="treeViewSelectedNodeStyles"
VerticalPadding="0px" Font-Underline="False"></SelectedNodeStyle>
<NodeStyle NodeSpacing="0px" HorizontalPadding="5px" ForeColor="Black" VerticalPadding="0px"
Font-Size="8pt" Font-Names="Verdana"></NodeStyle>
</asp:TreeView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
<td valign="middle" width="10%">
<asp:UpdatePanel ID="UpdatePanelButton" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table width="100%" align="center" border="0">
<!--功能按钮-->
<tbody>
<tr>
<td>
</td>
</tr>
<tr align="center">
<td style="height: 23px">
<asp:Button ID="ButtonAdd" OnClick="ButtonAdd_Click" runat="server" Text="添加" ></asp:Button></td>
</tr>
<tr>
<td>
</td>
</tr>
<tr align="center">
<td>
<asp:Button ID="ButtonRemove" OnClick="ButtonRemove_Click" runat="server" Text="删除" ></asp:Button></td>
</tr>
<tr>
<td>
</td>
</tr>
<tr align="center">
<td>
<asp:Button ID="ButtonClear" OnClick="ButtonClear_Click" runat="server" Text="清空"></asp:Button></td>
</tr>
<tr>
<td>
</td>
</tr>
<tr align="center">
<td>
<asp:Button ID="ButtonUp" OnClick="ButtonUp_Click" runat="server" Text="上移"></asp:Button></td>
</tr>
<tr align="center">
<td>
</td>
</tr>
<tr align="center">
<td>
<asp:Button ID="ButtonDown" OnClick="ButtonDown_Click" runat="server" Text="下移"></asp:Button></td>
</tr>
<tr>
<td>
</td>
</tr>
<tr align="center">
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</tbody>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td class="leftContentBorderStyles" valign="top" width="44%">
<!--已设置快捷的项-->
<div style="overflow: auto; width: 100%; height: 540px" id="DivWaitSelect">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListBox ID="ListBoxSelected" runat="server" Width="93%" Height="540px" ForeColor="Blue">
</asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
.cs 代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Infotech.MES.Web.MainPageReport
{
public partial class test : System.Web.UI.Page
{
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");
TreeNode Node0201 = new TreeNode("0201", "0201");
TreeNode Node0202 = new TreeNode("0202", "0202");
TreeNode Node020201 = new TreeNode("020201", "020201");
Node01.ChildNodes.Add(Node0101);
Node01.ChildNodes.Add(Node0102);
Node0201.ChildNodes.Add(Node020201);
Node02.ChildNodes.Add(Node0201);
Node02.ChildNodes.Add(Node0202);
Node00.ChildNodes.Add(Node01);
Node00.ChildNodes.Add(Node02);
TreeViewUserRight.Nodes.Add(Node00);
//添加树复选框的客户端点击事件
TreeViewUserRight.Attributes.Add("onclick", "postBackByObject()");
}
}
/// <summary>
/// 添加按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonAdd_Click(object sender, EventArgs e)
{
if (TreeViewUserRight.SelectedNode != null && TreeViewUserRight.SelectedNode.ChildNodes.Count < 1)
{
AddNode_Item(TreeViewUserRight.SelectedNode);
}
}
/// <summary>
/// 删除按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonRemove_Click(object sender, EventArgs e)
{
if (ListBoxSelected.Items.Count > 0 && ListBoxSelected.SelectedItem != null)
{
ListBoxSelected.Items.Remove(ListBoxSelected.SelectedItem);
}
}
/// <summary>
/// 清除按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonClear_Click(object sender, EventArgs e)
{
ListBoxSelected.Items.Clear();
}
/// <summary>
/// 上移按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonUp_Click(object sender, EventArgs e)
{
//位移量
int index = -1;
if (ListBoxSelected.SelectedItem != null && ListBoxSelected.SelectedIndex > 0)
{
UpAndDownItem(index);
}
}
/// <summary>
/// 下移按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonDown_Click(object sender, EventArgs e)
{
//位移量
int index = 1;
if (ListBoxSelected.SelectedItem != null && ListBoxSelected.SelectedIndex < ListBoxSelected.Items.Count - 1)
{
UpAndDownItem(index);
}
}
/// <summary>
/// 上下移动LISTBOX的项
/// </summary>
/// <param name="index">移动项索引</param>
private void UpAndDownItem(int index)
{
//临时保存当前选择的项
ListItem tempLt = new ListItem(ListBoxSelected.SelectedItem.Text, ListBoxSelected.SelectedItem.Value);
ListBoxSelected.Items[ListBoxSelected.SelectedIndex].Text = ListBoxSelected.Items[ListBoxSelected.SelectedIndex + index].Text;
ListBoxSelected.Items[ListBoxSelected.SelectedIndex].Value = ListBoxSelected.Items[ListBoxSelected.SelectedIndex + index].Value;
ListBoxSelected.Items[ListBoxSelected.SelectedIndex + index].Text = tempLt.Text;
ListBoxSelected.Items[ListBoxSelected.SelectedIndex + index].Value = tempLt.Value;
ListBoxSelected.SelectedIndex = ListBoxSelected.SelectedIndex + index;
}
/// <summary>
/// 添加选择的树节点到LISTBOX项
/// </summary>
/// <param name="node">选择的树节点</param>
private void AddNode_Item(TreeNode node)
{
if (ListBoxSelected.Items.Count > 0)
{
if (ListBoxSelected.Items.FindByValue(node.Value) == null)
{
ListBoxSelected.Items.Add(new ListItem(node.Text, node.Value));
}
}
else
{
ListBoxSelected.Items.Add(new ListItem(node.Text, node.Value));
}
}
/// <summary>
/// 树节点复选框的选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TreeViewUserRight_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
if (e.Node.Checked)
{
if (e.Node.ChildNodes.Count <= 0)//判断是否选择的是叶子节点
{
AddNode_Item(e.Node);
}
else
{
e.Node.Checked = false;
ContextHelper.ShowMessage(UpdatePanelManager, "只能选择叶子节点!");
}
}
}
}