I have a treeview and i want no postback on click any childnodes.And i will get selected childnode value.
I found a solution , when i set "node_.SelectAction = TreeNodeSelectAction.None;" i cant select any childnodes and no highlight on it.
Waiting your helps.Sorry about my en.
Aspx:
<asp:TreeView ID="TreeView1" runat="server"></asp:TreeView>
Cs:
TreeView1.Nodes.Clear();
TreeView1.TreeNodeExpanded += new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
DataTable dt = ImzaDll.Imza.KategorileriGetir(true);
foreach (DataRow row in dt.Rows)
{
TreeNode node_ = new TreeNode();
node_.Text = row["ACIKLAMA"].ToString();
node_.Value = row["KATEGORI"].ToString();
TreeView1.Nodes.Add(node_);
}
void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
addChildNodes(e.Node);
}
private void addChildNodes(TreeNode node)
{
DataTable dt = ImzaDll.Imza.KutuphaneBasliklariGetir(true, node.Value.ToString());
foreach (DataRow row in dt.Rows)
{
TreeNode childNode = new TreeNode();
childNode.Text = row["BASLIK"].ToString();
childNode.Value = row["KUTUPHANE_ID"].ToString();
childNode.ToolTip = row["BASLIK"].ToString() + " kütüphanesini ekle";
childNode.Target = "_new";
node.ChildNodes.Add(childNode);
}
}
You can set CSSClass of treeview child nodes
like
<asp:TreeView LeafNodeStyle-CssClass="childnode" runat="server">....</asp:TreeView>
then using jquery you get get class and set return false like follow.
$(".childnode").click(function(){
return false;
})
...same way you can set RootNodeStyle-CssClass, ParentNodeStyle-CssClass class and use jquery to set them...
TreeNode tn = new TreeNode();
tn.SelectAction = TreeNodeSelectAction.None; OR tn.SelectAction = TreeNodeSelectAction.Expand;
Both of these will not cause postback.
you could remove the href of link('a') tag attribute to stop post back
$('#ctl00_ContentPlaceHolder1_tvHierarchyView table tr td>a').click(function () {
var treeViewData = window["<%=tvHierarchyView.ClientID%>" + "_Data"];
if (treeViewData.selectedNodeID.value != "") {
var selectedNode=document.getElementById(treeViewData.selectedNodeID.value);
var value = selectedNode.href.substring(selectedNode.href.indexOf(",") + 3, selectedNode.href.length - 2);
var text = selectedNode.innerHTML;
alert("Text: " + text + "\r\n" + "Value: " + value);
} else {
alert("No node selected.")
}
$(this).removeAttr("href");
/// ...................... rest of your code
}); /// End of click function
}); /// End of document ready function
Here steps explanation:
Get the dev id which contains the tree table by Using inspect element:
Get details from the selected child node.
After taken the details of child node , remove the attribute "href" to avoid post back.
Do whatever functionality you what do with selected node details (eg pass selected value using ajax)
Related
I get data from tow database, db1,db2, then Now I want to create treeView for the data in the resulting datagridview; in datagrid view there are: id, name, director,the first record is the prim director, that mean he has not up director(he is owner), each record has no other record or has more records(child), and each child has grandchild and so on, this scenario Just Like in the this page:
I want to create treeview (parent and child and grandchild and so on), depending on xml file
when i used this snippet after some :
void setTree()
{
{
foreach(DataGridViewRow dt in DataGridView1.Rows)
{
var per = this.DataGridView1.Rows.Cast<DataGridViewRow>().Select(n => new person
{
name = dt.Cells[0].Value.ToString(),
Sex = dt.Cells[1].Value.ToString(),
Status = dt.Cells[2].Value.ToString(),
child = dt.Cells[3].Value.ToString(),
id = dt.Cells[4].Value.ToString(),
father = dt.Cells[5].Value.ToString()
}).ToList();
var rootTreeNode = GetTree(per, "").First();.........(1)
treeView1.Nodes.Add(rootTreeNode);
}
}
}
private TreeNode[] GetTree(List<person> per, string parent)
{
return per.Where(p => p.father == parent).Select(p =>
{
var node = new TreeNode(p.name);
node.Tag = p.id;
node.Nodes.AddRange(GetTree(per, p.id));
return node;
}).ToArray();
}
Now, when I use this code, I get error at mark(1),it say:Additional information: Sequence contains no elements.
thank you
after several readings in internet and attempts to solve this small problem, I successed finally.
this is the solution:
{
.............
TreeNode tn = new TreeNode(this.DataGridView2.Rows[0].Cells[0].Value.ToString());//text
tn.Tag = this.DataGridView2.Rows[0].Cells[4].Value.ToString();// id
tn.Name = this.DataGridView2.Rows[0].Cells[5].Value.ToString();//directorid
treeView1.Nodes.Add(tn);
settree(tn);
}
public void settree(TreeNode ns)
{
foreach (DataGridViewRow dr in DataGridView2.Rows)
{
if (dr.Cells[5].Value.ToString() == ns.Tag.ToString())
{
TreeNode tsn = new TreeNode(dr.Cells[0].Value.ToString());
tsn.Tag = dr.Cells[4].Value.ToString();
tsn.Name = dr.Cells[5].Value.ToString();
ns.Nodes.Add(tsn);
settree(tsn);
}
}
}
i will be happy if you benefit from this code.
I'm using a javascript menu from dynamicdrive
I'd tried to get the menu items from a database.
my aspx file contain the menu and it works fine when it's a static menu.
my table or my query actually produces:
IdPage int, PageTitle varchar(20), PageFileUrl varchar(30), ParentIdPage int
My methods to get data
DataRow[] dataRowParent = _dataTable.Select("[ParentIdPage]=" + 0);
foreach (DataRow dr in dataRowParent)
{
HtmlGenericControl li = new HtmlGenericControl("li");
// add <a>
HtmlGenericControl hlink = new HtmlGenericControl("a");
if (dr["PageFileUrl"].ToString() == "") // this item has a submenu.
{
li.Attributes.Add("rel", "ddsubmenu" + dr["IdPage"].ToString());
hlink.Attributes.Add("href", "#");// link should be # when no direct link
hlink.InnerText = dr["PageTitle"].ToString();
li.Controls.Add(hlink);
ulTopMenu.Controls.Add(li);
AddNewUl((int)dr["IdPage"]);
AddSubmenuItems(_dataTable, (int)dr["IdPage"]);
}
else // Direct link ,no submenu
{
hlink.Attributes.Add("href", dr["PageFileUrl"].ToString());
hlink.InnerText = dr["PageTitle"].ToString();
li.Controls.Add(hlink);
ulTopMenu.Controls.Add(li);
}
}
}
private void AddSubmenuItems(DataTable dataTable, int menuId)
{
// create related sub menu
DataView dataView = new DataView(dataTable);
dataView.RowFilter = "ParentIdPage=" + menuId;
foreach (DataRowView subMenuItem in dataView)
{
// find related <ul>
HtmlControl ulControl = (HtmlControl)FindControl("ddsubmenu" + menuId);
// Add new <li><a href="PageFileUrl.aspx" >page title</a> </li>
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlGenericControl hlink = new HtmlGenericControl("a");
hlink.Attributes.Add("href", subMenuItem["PageFileUrl"].ToString());
hlink.InnerText = subMenuItem["PageTitle"].ToString();
li.Controls.Add(hlink);
li.InnerText = subMenuItem["PageTitle"].ToString();
li.Attributes.Add("href", subMenuItem["PageFileUrl"].ToString());
ulControl.Controls.Add(li);
}
}
private void AddNewUl(int menuId)
{
// Add new <ul id="ddsubmenu00" class= "ddsubmenustyle">
HtmlGenericControl newUl = new HtmlGenericControl("ul");
// Set the attributes of the new ul.
newUl.ID = "ddsubmenu" + menuId;
newUl.Attributes.Add("class", "ddsubmenustyle");
placeHolder1.Controls.Add(newUl);
}
My problem is that submenu doesn't appear!, what is wrong?
any help appreciated.
Thanks for all, finally I figured it out:
Replace this line
li.Attributes.Add("rel",""+ "ddsubmenu" + dr["IdPage"].ToString()+"");
with this one:
hlink.Attributes.Add("rel",""+ "ddsubmenu" + dr["IdPage"].ToString()+"");
Now it works properly :-)
If the logic within this method is run from an event handler such as Button_Click it works perfectly, but, when running this from a method such as below I get the error:
hostView.SelectedNode.Nodes.Add(newNode);
Object reference not set to an instance of an object.
Here is my code:
private void SetupHostTree()
{
// Set internal host names
using (var reader = File.OpenText("Configuration.ini"))
{
List<string> hostnames = ParseInternalHosts(reader).ToList();
foreach (string s in hostnames)
{
TreeNode newNode = new TreeNode(s);
hostView.SelectedNode.Nodes.Add(newNode);
string title = s;
TabPage myTabPage = new TabPage(title);
myTabPage.Name = s;
tabControl1.TabPages.Add(myTabPage);
}
}
}
Maybe there are no Selected Nodes :)
Probably because no node is currently selected in the hostView TreeView.
The documentation says that the TreeView.SelectedNode property will return null when no node is currently selected. And since you've combined it into an expression, the entire expression is failing because there is no Nodes collection on a null object!
Try this code:
private void SetupHostTree()
{
// Set internal host names
using (var reader = File.OpenText("Configuration.ini"))
{
List<string> hostnames = ParseInternalHosts(reader).ToList();
foreach (string s in hostnames)
{
// Ensure that a node is currently selected
TreeNode selectedNode = hostView.SelectedNode;
if (selectedNode != null)
{
TreeNode newNode = new TreeNode(s);
selectedNode.Nodes.Add(newNode);
}
else
{
// maybe do nothing, or maybe add the new node to the root
}
string title = s;
TabPage myTabPage = new TabPage(title);
myTabPage.Name = s;
tabControl1.TabPages.Add(myTabPage);
}
}
}
I'm having a dataview which is collection of id & name. And i want to bind that dataview to the treeview control in the form of hirarchy. And i want to show the name field as the display text on the node & i want to bind id as a value member to the tree node.
IS it possible? If yes then how to do this?
thanks..
This snippet will display a dataview in a tree, where each row is a new node with children having column names followed by the field data for that column in the row:
var datasource = myDataView.ToTable();
treeView.BeginUpdate();
// Iterate throght the DataRow Collection
foreach (DataRow Row in datasource.Rows)
{
TreeNode Node = treeView.Nodes.Add("Node for "+ Row.Field<string>("ColumnNameForNode"));
if (Node != null)
{
int iCol = 0;
foreach (var item in Row.ItemArray)
{
string itemString = item as string;
if (itemString != null && itemString.Length > 0)
{
Node.Nodes.Add(datasource.Columns[iCol].ColumnName + " - " + itemString);
}
iCol++;
}
}
}
treeView.EndUpdate();
Is there a way I can keep the text of a Parent node but remove the link? The treeview parent node is used just as a header, no navigation. Any help would be great. Thanks.
private void BindNodes(string PtID)
{
if (PtID != "")
{
int PatientID = Convert.ToInt32(PtID);
DBConnection dbObj = new DBConnection();
if (Session["Activepatient"] != null)
{
string[] SubChild = new string[4];
SubChild[0] = "Demographics";
SubChild[1] = "Medication Reviews";
SubChild[2] = "Drug Testing & Monitoring";
SubChild[3] = "Other Program";
TreeNode oTrParent = new TreeNode();
//trv_patient.ParentNodeStyle = "None";
//oTrParent.SelectAction.Style.Add("display", "none");
TreeNode oTrSubChild1;
TreeNode oTrSubChild;
for (int i = 0; i < 4; i++)
{
oTrSubChild1 = new TreeNode();
oTrSubChild1.Text = SubChild[i];
if (i == 1)
{
PatientInfoCollection patientCollection = new PatientInfoCollection();
patientCollection = dbObj.GetMedicationReviews(PatientID);
foreach (PatientInfo au in patientCollection)
{
oTrSubChild = new TreeNode();
PatientInfo Patient = new PatientInfo();
oTrSubChild.Text = au.DateRcvd.Value.ToString("MM-dd-yyyy")
oTrSubChild.Target = au.ReviewId.ToString();
oTrSubChild1.ChildNodes.Add(oTrSubChild);
}
oTrSubChild = new TreeNode();
oTrSubChild.Text = "Add Medication Review";
oTrSubChild.Target = "New";
oTrSubChild1.ChildNodes.Add(oTrSubChild);
}
trv_patient.Nodes.Add(oTrSubChild1);
}
}
}
}
If temp is such node whose text you want to be plain text and not link then use code below.
TreeNode temp = new TreeNode("text");
temp.SelectAction = TreeNodeSelectAction.None;
treeView1.Nodes.Add(temp);
This is what your looking for. You have to set the SelectAction of the parent node TreeNode to TreeNodeSelectAction.None. I even show how to do it with the child too.
ASP.NET
<asp:TreeView
ID="MyTreeView"
ShowCheckBoxes="Parent,Leaf,All"
runat="server"
ShowLines="true"
ShowExpandCollapse="true">
</asp:TreeView>
C#
//Make A Parent Node
var Parent = new TreeNode();
Parent.Text = "Parent 1";
// Make the parent nodes not be hyperlinks but plain text
Parent.SelectAction = TreeNodeSelectAction.None;
//You can do the same with a child node like so
var Child = new TreeNode();
Child.Text = "Child 1";
// Make the child nodes not be hyperlinks but plain text
Child.SelectAction = TreeNodeSelectAction.None;
//Add the child node to the parent node
Parent.ChildNodes.Add(Child);
//Finally add the parent node with children to the treeview
MyTreeView.Nodes.Add(Parent);
I'm not sure if I understand what you want. Assuming that it's WinForms and that you just want to disable the ability to select the root node in a TreeView you could just handle the BeforeSelect event of the TreeView and then have the following code:
if (e.Node.Parent == null)
e.Cancel = true;
TreeNode root = new TreeNode("Root");
root.SelectAction = TreeNodeSelectAction.None;