Ok I add this as another answer because the comment box is to small,
To hide/show an item while dragging you can use the start and stop events.
This example shows/hides the +/- (collapse/uncollapse) icon next to the treenode text while dragging but you can easily modify it to hide/show the checkboxes if present.
<script type="text/javascript">
$(function () {
$(".treeNode").draggable({
start: function (event, ui) {
var previousTd = $(this).parent().prev() ;
$("img", previousTd).css("visibility", "hidden");
},
stop: function (event, ui) {
var previousTd = $(this).parent().prev();
$("img", previousTd).css("visibility", "visible");
});
$("#<%= TreeView1.ClientID%>").droppable({
drop: function (event, ui) {
alert(ui.draggable.text());
}
});
});
I'll try to have a look at the clone problem.
-- edit --
Apparently the clone problem is only in IE and is caused by the
<NodeStyle CssClass="treeNode" />
In conjunction with
$(".treeNode").draggable(...
This puts the treeNode class not only on the "a" tag but also on the surrounding "td" tag. So by using the .treeNode selector the draggable method is executed twice ... This is not the case in FF.
You could solve this by just changing the selector in "#<%= TreeView1.ClientID%> a.treeNode"
So with the afore mentioned aspx you would get the following script.
$(function () {
$("#<%= TreeView1.ClientID%> a.treeNode").draggable({
appendTo: 'body',
helper: 'clone',
start: function (event, ui) {
var previousTd = $(this).parent().prev();
$("img", previousTd).css("visibility", "hidden");
},
stop: function (event, ui) {
var previousTd = $(this).parent().prev();
$("img", previousTd).css("visibility", "visible");
},
zIndex: '100'
});
$("#<%= TreeView1.ClientID%>").droppable({
drop: function (event, ui) {
alert(ui.draggable.text());
}
});
});
</script>
-- edit --
In answer to your comment:
To get the value of the dragged node you can use javascript string manipulation to filter it out of the href attribute (treenode renders the value in the href attr).
Your javascript in the drop function could look like the following. (still need to do some checking for null values ofcourse)
var hrefParts = $(ui.draggable.context.href.split("\\"));
var nodeValue = hrefParts[hrefParts.length - 1];
nodeValue = nodeValue.substring(0, nodeValue.length - 2);
alert(nodeValue);
Or you can make it a little cleaner client-side by inheriting Treenode and wrap every Treenode by a div with a custom attribut in which you put your clientside id.
Your custom treenode could look like this
public class WrappedTreeNode : TreeNode
{
public string ClientValue { get; set; }
protected override void RenderPreText(HtmlTextWriter writer)
{
writer.WriteBeginTag("div");
//you can simply use the base.Value aswell here if you want them to be the same
writer.WriteAttribute("id", ClientValue);
base.RenderPreText(writer);
}
protected override void RenderPostText(HtmlTextWriter writer)
{
base.RenderPostText(writer);
writer.WriteEndTag("div");
}
}
Then in your aspx use (register you custom web control assembly first)
<ServerControls:WrappedTreeNode Text="Bradley" ClientValue="ID-1221"/>
instead of
<asp:TreeNode Text="Bradley" Value="ID-1221"/>
And your javascript stays nice and clean
alert($(ui.draggable).parent().attr("id"));