2012-12-06 28 views
0

我動態地添加一個jQuery的多選控制到這樣的頁面:的jQuery的document.ready拋出錯誤

var captionCell = new HtmlTableCell { InnerHtml = control.Caption }; 
var inputCell = new HtmlTableCell(); 
inputCell.Controls.Add(inputControl); 
var row = new HtmlTableRow(); 
row.Cells.Add(captionCell); 
row.Cells.Add(inputCell); 
tbl.Rows.Add(row); 

和建築我的JavaScript字符串是這樣的:

StringBuilder sb = new StringBuilder(); 
sb.AppendLine("<script type=\"text/javascript\">"); 
sb.AppendLine("var $callback = $(\"#callback\");"); 
sb.AppendLine("$(document).ready(function() {"); 
sb.Append("$(\"#"); 
sb.Append(multiSelectControl.ClientID); 
sb.AppendLine("\").multiselect("); 
sb.AppendLine("{"); 
sb.AppendLine("show: \"fade\","); 
sb.AppendLine("hide: \"fade\","); 
sb.AppendLine("click: function (event, ui){"); 
sb.AppendLine("$callback.text(ui.text + ' ' + (ui.checked ? 'checked' : 'unchecked'));"); 
sb.AppendLine("},"); 
sb.AppendLine("});"); 
sb.AppendLine("});"); 
sb.AppendLine("</script>"); 

然後添加腳本該頁面是這樣的:

ScriptManager.RegisterClientScriptBlock(this.Page, Page.GetType(), "CreateControl" + inputControl.ClientID, 
          sb.ToString(), false); 

但即時得到試圖做到這一點時,出現以下錯誤:

Microsoft JScript runtime error: Object doesn't support this property or method

請大家幫忙。

在此先感謝。

enter image description here

+1

看來你還沒有添加jQuery庫引用 – Rab

+1

sb.AppendLine(「},」);這似乎是一個額外的逗號在這裏... – Anujith

+0

檢查控制檯究竟發生了什麼錯誤.. –

回答

0

我解決了這個通過創建一個用戶控件,當我需要用我上面的腳本只是加載的用戶控件。

解決了所有問題,工作得很好。

1

您必須包含在使用的document.ready頁面jQuery的,您沒有添加script標籤在頁面中包含的jQuery,添加腳本標記包括jQuery的。

<script type="text/javascript" src="/jQueryFolder/jquery.js"></script> 
+0

我在Page_Load事件中做了如下操作: ScriptManager.RegisterClientScriptInclude (this.Page,Page.GetType(),「1」,「../../Scripts/jquery/jquery.multiselect.js」); ScriptManager.RegisterClientScriptInclude(this.Page,Page.GetType(),「2」,「../../Scripts/jquery/jquery.ui.js」); ScriptManager.RegisterClientScriptInclude(this.Page,Page.GetType(),「3」,「../../Scripts/jquery/jquery.latest.js」); – johnnie

+1

@johnnie首先加載jquery.latest,然後加載jquery ui,然後再加載其他東西。 –

+0

如果您不決定在運行時包含什麼,則可以簡單地使用腳本標記,而不使用RegisterClientScriptInclude。 – Adil

0
var $callback = $("#callback"); 
$(document).ready(function() { 
    $("#ClientID").multiselect({ 
    show: "fade", 
    hide: "fade", 
    click: function(event, ui) { 
     $callback.text(ui.text + ' ' + (ui.checked ? 'checked' : 'unchecked')); 
    } 
}); 
});​ 
+0

仍然收到與javascript相同的錯誤。 – johnnie