Show an organizational hierarchy in a treeview. for example, company -> department1, department2 etc, Every treenode has a checkbox displayed next to it.
Whenever the parent node is checked, all its child nodes will get checked as a result of it. I want to handle this event on the server side.
#1 is easy to do, but I can't figure out how to do #2 because there is no event gets fired for the checkbox click. Is there any way to fire this event to the server? Any help is greatly appreciated!!!
Thank you, Amanda, for your post, I just would change two little things in order to make it compatible with other navigators (Firefox, etc..):
function postBackByObject(mEvent)
{
var o;
// Internet Explorer
if (mEvent.srcElement)
{
o = mEvent.srcElement;
}
// Netscape and Firefox
else if (mEvent.target)
{
o = mEvent.target;
}
if (o.tagName == "INPUT" && o.type == "checkbox")
{
__doPostBack("","");
}
}
and add the event parameter to the fucntion call:
onclick="javascript:postBackByObject(event)"
After doing what Amanda suggested, the next step was to wrap my TreeView control with an Ajax UpdatePanel in order to do Asynchronous postbacks and refresh only the Treview control, it was then when I realized that with Ajax I didn't need the Amanda's postBackByObject
approach anymore, I just attached a trigger from the UpdatePanel to a hidden button and that's it!, the button will do the Postback. See the code below:
yaoliu
0 Points
2 Posts
Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
May 10, 2007 08:03 PM|LINK
My requirement is quite simple:
- Show an organizational hierarchy in a treeview. for example, company -> department1, department2 etc, Every treenode has a checkbox displayed next to it.
- Whenever the parent node is checked, all its child nodes will get checked as a result of it. I want to handle this event on the server side.
#1 is easy to do, but I can't figure out how to do #2 because there is no event gets fired for the checkbox click. Is there any way to fire this event to the server? Any help is greatly appreciated!!!TreeNode checkbox AutoPostback
Amanda Wang ...
All-Star
30008 Points
3104 Posts
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
May 14, 2007 03:36 AM|LINK
Hi,
In the treeView you can use the TreeNodeCheckChanged event for the checkbox click.
In the MSDN's TreeView.TreeNodeCheckChanged Event , you could get more inforamtion.
if you find the the treeview could not PostBack, I am afraid you should require javascript. Below is a simple example:
<script language="javascript" type="text/javascript">
function postBackByObject()
{
var o = window.event.srcElement;
if (o.tagName == "INPUT" && o.type == "checkbox")
{
__doPostBack("","");
}
}
</script>
<asp:TreeView ID="TreeView1" EnableClientScript="true" onclick="javascript:postBackByObject()" runat="server" DataSourceID="SiteMapDataSource1" LineImagesFolder="~/TreeLineImages" ShowLines="True" OnTreeNodeDataBound="TreeView1_TreeNodeDataBound" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged" ShowCheckBoxes="All">
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
freeborn5
Member
4 Points
6 Posts
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
May 16, 2007 06:31 PM|LINK
Thanks So much I was stumped on this the javascript works like a charm
yaoliu
0 Points
2 Posts
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
May 29, 2007 05:06 PM|LINK
Thanks a lot, Amanda, that will work.
jeny_p
Member
2 Points
1 Post
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
Jul 03, 2007 12:18 PM|LINK
Hi, I have the same problem but this code didn't work for me and I got the warning "Attribute onclick is not a valid attribute of element TreeView"
What might the problem be?
thanks
VinBrown
Member
39 Points
23 Posts
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
Jul 19, 2007 12:26 PM|LINK
Amanda, you are awesome. I needed a quick solution to this problem and this worked great.
abcpaem
Member
4 Points
2 Posts
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
Aug 24, 2007 08:54 AM|LINK
Thank you, Amanda, for your post, I just would change two little things in order to make it compatible with other navigators (Firefox, etc..):
function postBackByObject(mEvent) { var o; // Internet Explorer if (mEvent.srcElement) { o = mEvent.srcElement; } // Netscape and Firefox else if (mEvent.target) { o = mEvent.target; } if (o.tagName == "INPUT" && o.type == "checkbox") { __doPostBack("",""); } }and add the event parameter to the fucntion call: onclick="javascript:postBackByObject(event)"Thanks a lot!
Patrick.
abcpaem
Member
4 Points
2 Posts
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
Aug 24, 2007 04:16 PM|LINK
Hi again,
After doing what Amanda suggested, the next step was to wrap my TreeView control with an Ajax UpdatePanel in order to do Asynchronous postbacks and refresh only the Treview control, it was then when I realized that with Ajax I didn't need the Amanda's postBackByObject approach anymore, I just attached a trigger from the UpdatePanel to a hidden button and that's it!, the button will do the Postback. See the code below:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:TreeView ID="TreeViewMenu" runat="server" DataSourceID="XmlDataSourceMenu" ShowCheckBoxes="All" BorderWidth="3px" BorderColor="White" NodeStyle-ForeColor="#5982A2" OnTreeNodeDataBound="TreeViewMenu_TreeNodeDataBound" OnPreRender="TreeViewMenu_PreRender" OnTreeNodeCheckChanged="TreeViewMenu_TreeNodeCheckChanged"> <DataBindings> <asp:TreeNodeBinding DataMember="Menu" TextField="Name" Depth="0" SelectAction="None" ValueField="Id" /> <asp:TreeNodeBinding DataMember="Submenu" TextField="Name" Depth="1" SelectAction="None" ValueField="Id" /> </DataBindings> <NodeStyle ForeColor="#5982A2" /> </asp:TreeView> <asp:XmlDataSource ID="XmlDataSourceMenu" runat="server" DataFile="~/_resources/Menu.xml" XPath="//Menu" TransformFile="~/_resources/Menu.xslt"> </asp:XmlDataSource> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="buttonCheck" EventName="click" /> </Triggers> </asp:UpdatePanel> <asp:button ID="buttonCheck" runat="server" CausesValidation="false" />and the code behind is:
Of course there is no need to say that you must have an ScriptManager control for Ajax to work.
Patrick.
Omkar Lale
Participant
1288 Points
223 Posts
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
Sep 01, 2007 12:31 PM|LINK
Hi amanda and patrick,
Thanks a lot
I was facing this same problem of treeview.
I had tried out with what you both had suggested. And its working perfectly fine.
I am now using the code that patric had suggested since it doesn't not flash the page due to ajax.
Thanks again
Omkar
Omkar A. Lale
Keep your words soft and arguments hard.
~Do Mark as Answer if it solves your query~
sudhi345
Member
16 Points
12 Posts
Re: Asp.Net 2.0 Treeview - How to AutoPostback when a TreeNode's checkbox gets checked?
Jun 25, 2009 10:38 AM|LINK
buttonCheck.Attributes.CssStyle["visibility"] = "hidden"; TreeViewMenu.Attributes.Add("onclick", string.Format("document.getElementById('{0}').click();", buttonCheck.ClientID));
Need an equivalent code in VB.NET
Thanks in advance