ASP.net怎么给动态生成的TreeView控件加js事件. 本来有默认的超链接,Onclick,我要改掉他怎么改??
答案:5 悬赏:10 手机版
解决时间 2021-02-16 13:29
- 提问者网友:轮囘Li巡影
- 2021-02-16 08:33
ASP.net怎么给动态生成的TreeView控件加js事件. 本来有默认的超链接,Onclick,我要改掉他怎么改??
最佳答案
- 五星知识达人网友:一秋
- 2021-02-16 08:42
你不要用这种树,这样会对整个页面进行提交,你用那种传统的后台管理不行吗?
全部回答
- 1楼网友:酒者煙囻
- 2021-02-16 11:04
TreeView1.Attributes.Add("onclick", "OnTreeNodeChecked(event)");
OnTreeNodeChecked 为js事件;
如:aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
treeview父子节点联动
.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;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
if (!IsPostBack) {
CreateData();
BindTree(dtlModel.Select("pid='0'"), null);
TreeView1.Attributes.Add("onclick", "OnTreeNodeChecked(event)");
}
}
DataTable dtlModel = new DataTable();
private void CreateData() {
dtlModel.Columns.Add("id");
dtlModel.Columns.Add("pid");
dtlModel.Columns.Add("name");
for (int i = 1; i < 6; i++) {
DataRow dr = dtlModel.NewRow();
dr["id"] = i.ToString ();
dr["pid"] = "0";
dr["name"] = i.ToString()+"cun11";
dtlModel.Rows.Add(dr);
for (int j = 1; j < 6; j++) {
DataRow dr1 = dtlModel.NewRow();
dr1["id"] = (i.ToString() + j.ToString());
dr1["pid"] = i.ToString ();
dr1["name"] = i + "_" + j;
dtlModel.Rows.Add(dr1);
for (int k = 1; k < 6; k++) {
DataRow dr2 = dtlModel.NewRow();
dr2["id"] = i.ToString() + j.ToString() + j.ToString();
dr2["pid"] = dr1["id"];
dr2["name"] = i + "_" + j + "_" + k;
dtlModel.Rows.Add(dr2);
}
}
}
}
private void BindTree(DataRow[] rows, TreeNode node) {
foreach (DataRow r in rows) {
if (r["pid"] == DBNull.Value)
continue;
//如果是根结点,添加到TreeView控件
if (r["pid"].ToString() == "0") {
TreeNode node1 = new TreeNode();
node1.Text = string.Format("{2}",r["id"], r["id"], r["name"].ToString());
node1.Value = r["id"].ToString()+"zhaocj";
//node1.Target = "top";
TreeView1.Nodes.Add(node1);
BindTree(dtlModel.Select("pid=" + r["id"]), node1);
}
else if (node != null) {//否则添加到当前结点
TreeNode node1 = new TreeNode();
node1.Text = string.Format("{1}", r["id"], r["name"].ToString()); ;
node1.Value = r["id"].ToString() + "zhaocj";
node1.Target = "top";
node.ChildNodes.Add(node1);
BindTree(dtlModel.Select("pid=" + r["id"].ToString ()), node1);//递归生成子树
}
}
}
}
OnTreeNodeChecked 为js事件;
如:aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
.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;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
if (!IsPostBack) {
CreateData();
BindTree(dtlModel.Select("pid='0'"), null);
TreeView1.Attributes.Add("onclick", "OnTreeNodeChecked(event)");
}
}
DataTable dtlModel = new DataTable();
private void CreateData() {
dtlModel.Columns.Add("id");
dtlModel.Columns.Add("pid");
dtlModel.Columns.Add("name");
for (int i = 1; i < 6; i++) {
DataRow dr = dtlModel.NewRow();
dr["id"] = i.ToString ();
dr["pid"] = "0";
dr["name"] = i.ToString()+"cun11";
dtlModel.Rows.Add(dr);
for (int j = 1; j < 6; j++) {
DataRow dr1 = dtlModel.NewRow();
dr1["id"] = (i.ToString() + j.ToString());
dr1["pid"] = i.ToString ();
dr1["name"] = i + "_" + j;
dtlModel.Rows.Add(dr1);
for (int k = 1; k < 6; k++) {
DataRow dr2 = dtlModel.NewRow();
dr2["id"] = i.ToString() + j.ToString() + j.ToString();
dr2["pid"] = dr1["id"];
dr2["name"] = i + "_" + j + "_" + k;
dtlModel.Rows.Add(dr2);
}
}
}
}
private void BindTree(DataRow[] rows, TreeNode node) {
foreach (DataRow r in rows) {
if (r["pid"] == DBNull.Value)
continue;
//如果是根结点,添加到TreeView控件
if (r["pid"].ToString() == "0") {
TreeNode node1 = new TreeNode();
node1.Text = string.Format("{2}",r["id"], r["id"], r["name"].ToString());
node1.Value = r["id"].ToString()+"zhaocj";
//node1.Target = "top";
TreeView1.Nodes.Add(node1);
BindTree(dtlModel.Select("pid=" + r["id"]), node1);
}
else if (node != null) {//否则添加到当前结点
TreeNode node1 = new TreeNode();
node1.Text = string.Format("{1}", r["id"], r["name"].ToString()); ;
node1.Value = r["id"].ToString() + "zhaocj";
node1.Target = "top";
node.ChildNodes.Add(node1);
BindTree(dtlModel.Select("pid=" + r["id"].ToString ()), node1);//递归生成子树
}
}
}
}
- 2楼网友:十年萤火照君眠
- 2021-02-16 10:56
绑定的时候 动态给属性不就行了吗
- 3楼网友:妄饮晩冬酒
- 2021-02-16 09:43
你这是应该是空间吧?她应该有对应属性
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯