求助:TreeView的CheckBox勾选问题
TreeView里,ShowCheckBoxes = All,TreeView中使用CheckBox.当勾选之后,按Button將已勾的TreeView的节点值text取出來。
怎么写啊? --------------------编程问答-------------------- 遍历判断. --------------------编程问答-------------------- 谁给个例子把 --------------------编程问答-------------------- 自己尝试写一下嘛
就是把整个treeview的nodes循环一次,判断它的是否勾选,如果勾选了的就把它的text值存起来 --------------------编程问答-------------------- 我给你
--------------------编程问答--------------------
刚学C#,我如果会写的话,就不用问了 --------------------编程问答-------------------- protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataSource = CreateDataSource();
this.GridView1.DataBind();
}
}
ICollection CreateDataSource()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("name", typeof(System.String)));
for (int i = 0; i < 10; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = i.ToString();
dr[1] = "中央" + i.ToString();
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox3.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
//this.Response.Write("ok3");
}
protected void Button2_Click(object sender, EventArgs e)
{
string tmp = "";
int sum = 0;
foreach(GridViewRow gvr in GridView1.Rows)
{
CheckBox ch = (CheckBox)gvr.FindControl("CheckBox1");
if (ch.Checked==true)
{
sum++;
string strTmp=this.GridView1.DataKeys[gvr.DataItemIndex].Value.ToString();
if (sum == 1)
{
tmp = strTmp;
}
else
{
tmp += "," + strTmp;
}
}
}
//this.Response.Write(tmp);
if (sum > 0)
{
this.Response.Redirect("dowithstring.aspx?strids=" + tmp);
}
else
{
this.Response.Write("<script language='javascript'>alert('请选择!!')</script>"); }
}
--------------------编程问答-------------------- 唉,看错了。再来 --------------------编程问答-------------------- 选中子节点 则父节点被选中。选中父节点所有子节点被选中,取消父节点,子节点将不被选中.
<asp:TreeView ID ="tvTemplate" runat ="server"
onclick="client_OnTreeNodeChecked(event)"
onselectednodechanged="tvTemplate_SelectedNodeChanged" >
</asp:TreeView>
function client_OnTreeNodeChecked(evt){
var obj = getCurrentNode(evt);
var treeNodeFound = false;
var checkedState;
if (obj.tagName == "INPUT" && obj.type == "checkbox" ){
checkedState = obj.checked;
var curNode = getParentNode(obj);
changeNodeState(curNode,checkedState);
}
}
function getCurrentNode(evt){
var obj;
if(window.event) obj = window.event.srcElement;
else obj = (evt ? evt : (window.event ? window.event : null)).target;
return obj;
}
function getParentNode(node){
do
{
node = node.parentNode;
}
while (node.tagName != "TABLE")
return node;
}
function changeNodeState(node,state){
//change all the children
if(node.nextSibling!=null && node.nextSibling.tagName=="DIV"){
var cbArr = node.nextSibling.getElementsByTagName("INPUT");
for(var i=0; i<cbArr.length; i++){
if(cbArr[i].type == "checkbox") cbArr[i].checked = state;
}
}
//change its parents' state
// var flag = true;
// var inputArr = node.parentNode.getElementsByTagName("INPUT");
// if(state){
// for(var i=0; i<inputArr.length; i++){
// if(inputArr[i].type == "checkbox" && inputArr[i].checked) flag = false;
// }
// }
// else flag = true
if(state){
var parentArr = node.parentNode.previousSibling.getElementsByTagName("INPUT");
for( i=0; i<parentArr.length; i++){
if(parentArr[i].type == "checkbox") parentArr[i].checked = state;
}
}
} --------------------编程问答-------------------- 我再试一下 --------------------编程问答--------------------
public void AddTree(int ParentId, TreeNode ParentNode)
{
//................................
//过滤ParentID,得到当前的所有子节点
DvTree.RowFilter = "[ParentId] = " + ParentId;
foreach (DataRowView Row in DvTree)
{
TreeNode Node = new TreeNode();
if (ParentNode == null)
{
//添加根节点
Node.Text = Row["DepartmentName"].ToString();
TreeView1.Nodes.Add(Node);
Node.Expanded = true;
AddTree(Int32.Parse(Row["DepartmentId"].ToString()), Node); //再次递归
Node.NavigateUrl = "AddDepartment.aspx?TypeId=Update&DepartmentId=" + Int32.Parse(Row["DepartmentId"].ToString()) + "";
}
else
{
//添加当前节点的子节点
Node.Text = Row["DepartmentName"].ToString();
ParentNode.ChildNodes.Add(Node);
Node.Expanded = true;
AddTree(Int32.Parse(Row["DepartmentId"].ToString()), Node); //再次递归
Node.NavigateUrl = "AddDepartment.aspx?TypeId=Update&DepartmentId=" + Int32.Parse(Row["DepartmentId"].ToString()) + "";
}
}
}
上面我添加了一个Treeview
刚才没说清楚,我的意思是 勾选一个节点后,把这个节点的node.Text取出来
然后按button按钮后,通过取出的node.Text值,给其添加一个子节点。 --------------------编程问答-------------------- 继续求助
补充:.NET技术 , ASP.NET