ASTreeView – How to disable checkbox PostBack

By | May 15, 2010

There’s a request that to disable the checkbox PostBack while the AutoPostBack properties is set to true. Because for node selection, we want to enable the PostBack.

By default, if we set the AutoPostBack properties of ASTreeView, both the checkbox and selection will be effected.

So here we need a little tricky to achieve that:

First, in the aspx page, we need to remove the postback related attribute for the checkbox, here’s the javascript function you can put into your aspx page:

<script type="text/javascript">
	function clearCheckboxPostBackEvent(){
		var cbs = document.getElementsByTagName("IMG");
		for( var i = 0; i < cbs.length; i++ )
		{
			if( cbs[i].className == "astreeview-checkbox" ){
				cbs[i].setAttribute("postbackscript","");										
			}
		}	
	}
</script>

Then, in the code behind, in Page_Load event handler, you call the javascript function above:

protected void Page_Load( object sender, EventArgs e )
{
	string clearCheckboxPostbackScript = "clearCheckboxPostBackEvent();";
	ScriptManager sm = ScriptManager.GetCurrent( this );
	if( sm != null && sm.EnablePartialRendering )
		ScriptManager.RegisterStartupScript( this
										, this.GetType()
										, "clearCBPostback"
										, clearCheckboxPostbackScript
										, true );
	else
		ClientScript.RegisterStartupScript( this.GetType()
										, "clearCBPostback"
										, clearCheckboxPostbackScript
										, true );

}

That’s it, the result will be: the selection postback is activated and the checkbox won’t perform postback by clicking.