使用Checkbox帮助TreeView [英] Help in TreeView With Checkbox
本文介绍了使用Checkbox帮助TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们使用包含node1和子节点1.1,1.2,1.3,1.4的复选框进行树视图。
i想要这样做,当用户选择主节点自动选择的所有子节点时如果用户选择一些子节点,主节点以其他方式选择。就像一个填充的正方形。
我怎么能这样做??????
Suppose that we have treeview with checkbox including node1 and sub node 1.1,1.2,1.3,1.4.
i want to do this that when a user select all of the sub nodes the main node selected automatically and if user select some of the sub nodes the main node selected in other way.like a filled square.
how can i do this??????
推荐答案
先生,您需要循环遍历每个节点并检查是否所有子节点都被选中,如果是,则需要以编程方式检查父节点是否为真。
sir, you need to loop through every node and check if all the childs are selected and if yes you need to programitically check the parent true.
我认为这是Telerik RadTreeView中的默认功能。
http://demos.telerik.com/aspnet-ajax/treeview/examples/overview/defaultcs.aspx [ ^ ]
它也可能出现在asp TreeView中。尝试搜索,你就会知道。
问候,
Praneet
Hi,
I think this is the default functionality present in Telerik RadTreeView.
http://demos.telerik.com/aspnet-ajax/treeview/examples/overview/defaultcs.aspx[^]
It may also be present in asp TreeView. Try and search and you will know.
Regards,
Praneet
设计代码:< br $>
Desing code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
<script type="text/javascript">
function postBackByObject() {
var o = window.event.srcElement;
if (o.tagName == "INPUT" && o.type == "checkbox") {
__doPostBack("", "");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All"
AutoGenerateDataBindings="False" ExpandDepth="0"
EnableClientScript="true"
ontreenodepopulate="TreeView1_TreeNodePopulate"
ontreenodecheckchanged="TreeView1_TreeNodeCheckChanged"
onselectednodechanged="TreeView1_SelectedNodeChanged" >
</asp:TreeView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Cs代码:
Cs code :
public partial class Default2 : System.Web.UI.Page
{
DataTable dt = new DataTable();
private SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.Attributes.Add("onclick", "postBackByObject()");
if (!IsPostBack)
{
string cs = "server=sql2005 ;database=dbName; uid=sa;pwd=123";
con = new SqlConnection(cs);
con.Open();
SqlCommand cme = new SqlCommand("select * from tbName where parenttab=0 and roleid=1", con);
//cme.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cme);
DataSet ds = new DataSet();
da.Fill(ds, "T");
dt = ds.Tables["T"];
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode no = new TreeNode();
no.Text = dt.Rows[i]["LinkName"].ToString();
// no.SelectAction = TreeNodeSelectAction.SelectExpand;
no.Value = dt.Rows[i]["LinkId"].ToString();
int a = Convert.ToInt32(dt.Rows[i]["LinkId"]);
// int a = Convert.ToInt32(dt.Rows[i]["LinkId"]);
AddTrem(no,a);
this.TreeView1.Nodes.Add(no);
}
}
}
public void AddTrem(TreeNode no,int a)
{
SqlCommand cme = new SqlCommand("select * from tbName where parenttab="+a, con);
SqlDataAdapter da = new SqlDataAdapter(cme);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode childnode = new TreeNode();
childnode.Text = dt.Rows[i]["LinkName"].ToString();
childnode.Value = dt.Rows[i]["linkid"].ToString();
// AddSubject(childnode);
no.ChildNodes.Add(childnode);
}
}
}
}
这篇关于使用Checkbox帮助TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文