UPDATE的RegisterStartupScript在用戶控制不重新加載/改寫的Javascript
問題的總結:
有6個動態UCS其中產生並且每個都有其自己的自定義JS將其注入在頁面上使用RegisterStartUpScript初始化的UC
只有第一次生成第一個UC的父頁面將腳本正確放置在頁面的底部。
在後續部分更新(AJAX調用),以生成新的UC,相關的腳本不注入......
我會繼續搜索並嘗試不同的東西,但如果有人能指導我將不勝感激 謝謝!
下面是完整的解釋
我有使用其中RadScriptManager定義碩士(Telerik的腳本管理)父頁面。父頁面有一個佔位符,用於生成動態用戶控件。 UC有一個javascript,我在UC的Page_Init上注入。
當我第一次調用它時,第一頁加載,腳本正常工作..我可以在Chrome調試器中的源代碼中看到它。當我更改父母上的ddl時,它會調用並在佔位符內生成另一個UC。後者有自己的Javascript注入類似的第一個生成...兩個UC基本上是每個省的意見,所以它具有相同的結構,但形式內容略有不同...
問題是,當我查看Chrome調試器,我看到用戶COntrol內容的部分更新,但是我沒有看到第二個控制器的新腳本,它應該被添加到UC的Page_Init的add_load上。所以每次我改變控制,它調用前面的腳本......雖然我的代碼通過下面的部分......這基本上是爲正在加載的給定控件註冊適當的腳本。
在下面的例子中,我總是看到一條警告「SK cmbSearch = ..」,而不是「ON cmbSearch = ...」,這是我正試圖加載的當前省份。
任何人都可以給我一些指針嗎?
public void RegisterSearchVisibilityPPSA()
{
string jsFunction =
@"function DefineView(sender, eventArgs) {
var comboSearch = $find('%%cmbSearchType%%');
//cmbSearch Section
alert('ON cmbSearch = %%cmbSearchType%% with value = ' + comboSearch.get_selectedItem().get_value());
switch (comboSearch.get_selectedItem().get_value()) {
//PER
case 'IN':
case 'IS' :
$('#tblPersonSearchForm').show();
$('#tblPersonDOBSearchForm').show();
$('#tblBusinessSearchForm').hide();
$('#tblSerialNumberedSearchForm').hide();
break;
//BUS
case 'BD':
$('#tblPersonSearchForm').hide();
$('#tblPersonDOBSearchForm').hide();
$('#tblBusinessSearchForm').show();
$('#tblSerialNumberedSearchForm').hide();
break;
//SER
case 'MVE':
case 'MVS':
$('#tblPersonSearchForm').hide();
$('#tblPersonDOBSearchForm').hide();
$('#tblBusinessSearchForm').hide();
$('#tblSerialNumberedSearchForm').show();
break;
//REG
case 'REG':
default:
$('#tblPersonSearchForm').hide();
$('#tblPersonDOBSearchForm').hide();
$('#tblBusinessSearchForm').hide();
$('#tblSerialNumberedSearchForm').hide();
$('#tblRegistrationNumberedSearchForm').show();
break;
}
}
function onPageLoad(){
// jquery Area
$(document).ready(function() {
DefineView();
});
}
";
RadScriptManager.RegisterStartupScript(this, this.GetType(), "ON_SearchVisibilityPPSA",jsFunction.Replace("%%cmbSearchType%%", cmbSearchType.ClientID), cmbRegistrationType.ClientID), true);
string onLoadScript = @" Sys.Application.add_load(function(){ onPageLoad();}) ";
RadScriptManager.RegisterStartupScript(this, this.GetType(), "ON_onLoadScript", onLoadScript, true);
}
我設法通過確保腳本的鍵名是獨一無二的...所以在回發和部分回帖後,新的javascript寫入... – Gotcha
小心使用加載事件處理程序(add_load功能)。訂閱保留部分回發,所以處理程序可以很容易[註冊多次](http://jakub-linhart.blogspot.com/2012/04/script-registration-labyrinth-startup.html)。 –