I use treeview with checkboxes. On checkbox click node expands.
function InitTreeViewClick(){ $('#" + treevew.ClientID + @" :input').each(function(){ $(this).unbind(); var a=this.nextSibling; if(a.nodeName=='A') $(this).bind('click', function(){a.click();});});}
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InitTreeViewClick);
You can modify a.click() for example: setTimeout(function(){a.click();},3000);
In my case this works perfectly.
And cs file code:
protected void Page_PreRender(object sender, EventArgs e)
{
if (!this.Page.ClientScript.IsStartupScriptRegistered("treeview"))
{
string script = @"Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InitTreeViewClick);
function InitTreeViewClick(){ $('#" + treevew.ClientID + @" :input').each(function(){ $(this).unbind(); var a=this.nextSibling; if(a.nodeName=='A') $(this).bind('click', function(){a.click();});});} ";
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "treeview", script, true);
}
}
In your case you can find all links
$('#" + treevew.ClientID + @" ).find('a').each( function(){$(this).click(
// your logic
);});
OR
$('#" + treevew.ClientID + @" ).find('a').each(function(){$(this).click(function(){$(this).parentsUntil('table').parent('table').next().css('background','red').slideToggle("slow");});});
this code will slowly collapse and expand div with subitems when you click on parent node and background for child nodes will be red on this time.
Final solution (I applied this solution in my project too, because it looks fine).
ASCX file code:
<asp:TreeView ID="treeview" runat="server" ForeColor="Black" CssClass="inputs" ExpandDepth="0" MaxDataBindDepth="2"
EnableClientScript="true" ShowCheckBoxes="All" AutoGenerateDataBindings="false" ShowLines="false"
ShowExpandCollapse="false">
</asp:TreeView>
and cs file code:
if (!this.Page.ClientScript.IsStartupScriptRegistered("treeview")) { string script = @"function InitTreeView(){$('#" + treeview.ClientID + @"' ).find('a').each(function(){$(this).unbind().removeAttr('onclick').removeAttr('href').click(function(){$(this).parentsUntil('table').parent('table').next().slideToggle('slow');});});} Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InitTreeView);"; ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "treeview", script, true); }
I remove href attribute with __DoPostBack from tag anchor, because I need to save only checkboxes value.
Many thanks indeed user 1804985 for your question. :)