Tajuddin_HYD
Код проектирования:
<%@ 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 :
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);
}
}
}