單獨控制IE WebControl里面的Treeview的幾個動作的Postback
2024-07-21 02:23:17
供稿:網友
在使用這個treeview是經常需要將動作postback到服務器端來做一些數據操作,如onexpanded或者selectindexchange等。treeview控件提供了一個autopostback的屬性來設置這個treeview是否需要進行postback的動作來讓后臺操作數據。但是這個屬性一旦設成true的話,那么treeview的所有動作都會postback回去將頁面刷新,不管是expaned還是selectindexchangek都postback回去一次,很多時候我們只是需要將其中的某種事件postback回去而已。如果所有的事件都postback的話,不但影響用戶體驗還有影響系統性能。解決這個問題的方法是自己處理每種動作的postback,如果條件不成立就不postback,盡量減少不必要的postback。
下面就舉個例子講講怎么自己控制postback的動作。
protected microsoft.web.ui.webcontrols.treeview tvschema;
private void page_load(object sender, system.eventargs e)
{
if(!ispostback)
{
//修改expand客戶端事件的代碼
string script = @"javascript: if (this.clickednodeindex != null){
this.queueevent('onexpand', this.clickednodeindex);
expandedtable(this,this.clickednodeindex);
}
";
tvschema.attributes["onexpand"]=script;
}
//注冊控制腳本
registetreescript();
}
private void registetreescript()
{
string script
= @"<script language='javascript'>
function expandedtable(sender,_nodeindex)
{
var node=sender.gettreenode(_nodeindex);
//如果沒有子對象時就postback;
if(node.getchildren().length==0)
"+getpostbackeventreference(tvschema)[email protected]";
}
</script>";
this.registerclientscriptblock("tvschema",script);
}
private void tvschema_expand(object sender, microsoft.web.ui.webcontrols.treeviewclickeventargs e)
{
//根據選中的目標,自己生成子樹
}
在上面的代碼中registetreescript()函數里大部分都是客戶端的腳本而已,但是這些客戶端腳本不能夠觸發頁面的postback的,所以需要調用getpostbackeventreference(tvschema)來生成postback的腳本,不過這個函數的不只是返回__dopostback('tvschema','');這個字符串這么簡單。當你的頁面上沒有其他postback動作的webcontrol時,他就會自動生成那個__dopostback的客戶端腳本了。