Hello,
Can you please give me a concrete exemple of how can we add templates to nodes server side?
I need to get nodes ( with images and text) using templates like shown in the given page:
http://demos.telerik.com/aspnet-ajax/treeview/examples/functionality/templates/defaultcs.aspx.
Thank You & best Regards,
Hind
Can you please give me a concrete exemple of how can we add templates to nodes server side?
I need to get nodes ( with images and text) using templates like shown in the given page:
http://demos.telerik.com/aspnet-ajax/treeview/examples/functionality/templates/defaultcs.aspx.
Thank You & best Regards,
Hind
7 Answers, 1 is accepted
0
Accepted
Princy
Top achievements
Rank 2
answered on 18 Nov 2011, 12:39 PM
Hello,
You can try the following code snippet.
C#:
Thanks,
Princy.
You can try the following code snippet.
C#:
protected
override
void
OnInit(EventArgs e)
{
RadTreeView1.NodeTemplate =
new
TextBoxTemplate();
base
.OnInit(e);
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
RadTreeView1.Nodes.Add(
new
RadTreeNode(
"root1"
)); RadTreeView1.Nodes.Add(
new
RadTreeNode(
"root2"
));
}
RadTreeView1.DataBind();
}
class
TextBoxTemplate : ITemplate
{
public
void
InstantiateIn(Control container)
{
Image img =
new
Image();
img.ImageUrl =
"~/Images/edit.jpg"
;
img.ID =
"img1"
;
container.Controls.Add(img);
Label label1 =
new
Label();
label1.ID =
"ItemLabel"
;
label1.Text =
"Text"
;
label1.Font.Size = 10;
label1.Font.Bold =
true
;
label1.DataBinding +=
new
EventHandler(label1_DataBinding);
container.Controls.Add(label1);
}
private
void
label1_DataBinding(
object
sender, EventArgs e)
{
Label target = (Label)sender;
RadTreeNode node = (RadTreeNode)target.BindingContainer;
string
nodeText = (
string
)DataBinder.Eval(node,
"Text"
);
target.Text = nodeText;
}
}
Thanks,
Princy.
0
Hind
Top achievements
Rank 1
answered on 21 Nov 2011, 04:52 PM
Hello,
Thank You for your answer.
Furthermore, If I won't have NodeTemplates to all my TreeNodes, Should I as well initialize my templates before nodes? Or can I write for exemple (if I need in the exemple a template just for the root1):
Regards,
Hind
Thank You for your answer.
Furthermore, If I won't have NodeTemplates to all my TreeNodes, Should I as well initialize my templates before nodes? Or can I write for exemple (if I need in the exemple a template just for the root1):
if (!Page.IsPostBack)
{
RadTreeView1.Nodes.Add(new RadTreeNode("root1")); RadTreeView1.Nodes.Add(new RadTreeNode("root2"));
TextBoxTemplate
template1 = new TextBoxTemplate();
template1.InstantiateIn(root1);
}
RadTreeView1.DataBind();
}
class TextBoxTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
Image img = new Image();
img.ImageUrl = "~/Images/edit.jpg";
img.ID = "img1";
container.Controls.Add(img);
Label label1 = new Label();
label1.ID = "ItemLabel";
label1.Text = "Text";
label1.Font.Size = 10;
label1.Font.Bold = true;
label1.DataBinding += new EventHandler(label1_DataBinding);
container.Controls.Add(label1);
}
private void label1_DataBinding(object sender, EventArgs e)
{
Label target = (Label)sender;
RadTreeNode node = (RadTreeNode)target.BindingContainer;
string nodeText = (string)DataBinder.Eval(node, "Text");
target.Text = nodeText;
}
}
Regards,
Hind
0
Accepted
Hello Hind,
Yes, you can have a template only for one node, but you should create the root node- for example root1 before you add it like in the following code:
Hope this will be helpful.
Kind regards,
Plamen Zdravkov
the Telerik team
Yes, you can have a template only for one node, but you should create the root node- for example root1 before you add it like in the following code:
RadTreeNode root1 =
new
RadTreeNode(
"root1"
);
RadTreeView1.Nodes.Add(root1);
Hope this will be helpful.
Kind regards,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Hind
Top achievements
Rank 1
answered on 24 Nov 2011, 03:01 PM
Thanks for Your response,
It's work fine.
Just One more Question. It's about adding a background, for exemple to the node root1.
This snippet code shoudn't give the required result? Because it's not the case for me.
string sBckGrdPath = "url(../images/repeat_menu_task_sub.png)";
root1.Style.Add("background", sBckGrdPath + "repeat-y");
Regards,
Hind
It's work fine.
Just One more Question. It's about adding a background, for exemple to the node root1.
This snippet code shoudn't give the required result? Because it's not the case for me.
string sBckGrdPath = "url(../images/repeat_menu_task_sub.png)";
root1.Style.Add("background", sBckGrdPath + "repeat-y");
Regards,
Hind
0
Princy
Top achievements
Rank 2
answered on 25 Nov 2011, 06:22 AM
Hello,
I suppose you want to set background for nodes. So try the following code snippet.
CS:
Thanks,
Princy.
I suppose you want to set background for nodes. So try the following code snippet.
CS:
class
TextBoxTemplate : ITemplate
{
public
void
InstantiateIn(Control container)
{
...
...
...
label1.Style.Add(
"background"
,
"red"
);
container.Controls.Add(label1);
}
Thanks,
Princy.
0
Hind
Top achievements
Rank 1
answered on 28 Nov 2011, 11:32 AM
Hello Princy,
Can't I set a repeated image as a background for nodes?
Thanks,
Hind
Can't I set a repeated image as a background for nodes?
Thanks,
Hind
0
Accepted
Hello Hind,
You can try to set the background image with css as in the code:
Hope this helps.
Greetings,
Plamen Zdravkov
the Telerik team
You can try to set the background image with css as in the code:
.rtTemplate
{
background-image
:
url
(
'cloudy-overcast.png'
)
!important
;
}
Hope this helps.
Greetings,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now