2013-05-20 32 views
1

我想在c#代碼隱藏中使用Response.Write方法調用jQuery對話框。我試着用RegisterScriptBlockResponse.Write在c#代碼隱藏中調用jQuery對話框

Response.Write("<script> $(document).ready(function() {$('#divOutputWindow').html('You are not authorised to view this Page..!!').dialog({title: 'Notice...',show: 'slide',hide: 'blind',modal: true,buttons: {'Ok':function(){window.location = 'Default.aspx';}}});});</script>"); 

所有我的jQuery已經適當地包括,因爲我能夠從我的JS文件調用對話框。 ID:divOutputWindow存在於.aspx頁面

仍然無法看到jQuery對話框。

P.S. :我的.aspx頁面不包含<form>標記因此RegisterScriptBlock不能工作

+0

@RandomWebGuy但用戶新增<form id="frmUserManagement" runat="server">必須得到一個消息,所以他已經被重定向? – Shaggy

回答

0

This Worked .. !!

在我的aspx頁面

,並在代碼隱藏

StringBuilder strScript = new StringBuilder(); 
strScript.Append("$(document).ready(function(){"); 
strScript.Append("$('#divOutputWindow').html('You are not authorised to view this Page..!!<br/>Redirecting to Default Page.').dialog({title: 'Error...',show: 'slide',hide: 'blind',modal: true,buttons: {'Ok': function() {window.location='Default.aspx';}}});"); 
strScript.Append("});"); 
ScriptManager.RegisterStartupScript(frmUserManagement, frmUserManagement.GetType(), "Script", strScript.ToString(), true); 
2

它沒有工作,因爲Response.Write在頁面頂部添加了代碼塊。通過這種方式,該腳本在加載jq庫之前執行。 您需要使用ScriptManager和RegisterClientScriptBlock。

+0

你是對的.. !!我的'script'標籤在所有jQuery lib之前被加載到'head'標籤的下方。我怎麼能使用'RegisterClientScriptBlock'實現相同的功能?你能發佈嗎? – Shaggy

+0

對不起,但我工作,但我看到你找到了解決方案:) – Darkito