2
Answers

How to bind Tree View Node from database dynamically in asp

Hello,
 
I am creating a treeview in asp.net C# dybamically.
 
Data in tree view binding successfully , But Nodes is not creating to according to table data so that i could expend and collapse
 
<asp:TreeView ID="TreeView2" runat="server" ImageSet="XPFileExplorer"
NodeIndent="15" Expanded="True" Font-Names="Verdana" Font-Size="12px"
ForeColor="#F48110"
SkinID="NonPostBackTree" Height="100%" Width="415px"
SelectedNodeStyle-ForeColor="blue"
>
<%-- <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />--%>
<%-- <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>--%>
<%-- <ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />--%>
</asp:TreeView>
 
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData("Select distinct Pid, PName,cid,cname from TopParent");
this.PopulateTreeView(dt, 0, null);
BindRepeaterRight();
}
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["cName"].ToString(),
Value = row["cId"].ToString()
};
if (parentId == 0)
{
TreeView2.Nodes.Add(child);
DataTable dtChild = this.GetData("Select * from TopParent where PId= " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
// child.PopulateOnDemand = true;
child.SelectAction = TreeNodeSelectAction.SelectExpand;
// treeNode.ChildNodes.Add(child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
//DataSet ds = GetDataSet("select cid,cName from 'tablename where pname='0' and pId='" + strCompantid + "'");
//foreach (DataRow row in ds.Tables[0].Rows)
//{
// TreeNode node = new TreeNode();
// node.Text = row["DependencyName"].ToString();
// node.Value = row["Dependency"].ToString();
// node.PopulateOnDemand = true;
// node.SelectAction = TreeNodeSelectAction.SelectExpand;
// parent.ChildNodes.Add(node);
//}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
private void BindRepeaterRight()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM TopParent", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
reaptrright.DataSource = dt;
reaptrright.DataBind();
}
}
}
}
 
Suppose , i have a table in database. I want to bind The unique id and name in nodes and when i click on node , it must show the all data according to that id by expanding the nodes. How to do this.
 
below is my table-
Now Suppose when i click on India (its CID, Cid is qunique for each row ) it must be expand and show 3 child node as UP, HP, Kerala.
 
Please update my answere
Answers (2)
0
Sourabh Choubey

Sourabh Choubey

NA 186 9.3k 7y
AVINASH can i implement this in wpf webbrowser control and how??
0
Sourabh Choubey

Sourabh Choubey

NA 186 9.3k 7y
I AM USING WEBBROWSER CONTROL IN WPF .IS IT POSSIBLE TO LOAD THE HTM ELEMENT WHEN I CLICKED ON ANCHOR TAG??
0
Avinash Aher

Avinash Aher

NA 260 594.3k 7y
 $('a').click(function(e)
{      
   e.preventDefault();   
   $("#content").append('<div>Test Content</div>');
 });

OR 

 $('a').click(function(e)
{ 
   e.preventDefault(); 
   $("#content").load('test.html');
 });
and test.html have your own html which you want to load in page


0
rajendra singh

rajendra singh

NA 382 421 7y
You Want Load Html Content Of Div when you click on anchor tag if this right
 
so you can do with jquery using display none and block property 
0
Sourabh Choubey

Sourabh Choubey

NA 186 9.3k 7y
sorry it is official...i only want how to do that 
0
rajendra singh

rajendra singh

NA 382 421 7y
Can You Share Your Code