2010-04-08 46 views
0

我想添加一些共享此javascript在asp.net頁面的頭標記之間,但僅當頁面不安全(!Request.IsSecureConnection)時。如何獲取head標籤中的代碼來檢查安全連接,然後在不安全的情況下編寫javascript。我已經使用<%%>塊和RegisterStartupScriptBlock嘗試和它不工作ASP.NET如果頁面不安全,則將頁面添加到頁面

UPDATE:

能得到它在Page_Load使用該工作

if(!Request.IsSecureConnection) 
{ 
    HtmlGenericControl Include = new HtmlGenericControl("script"); 
    Include.Attributes.Add("type", "text/javascript"); 
    Include.Attributes.Add("src", "http...."); 
    this.Page.Header.Controls.Add(Include); 
} 
+0

你可以上傳你嘗試了一些特定的代碼。簡單地說「它不工作」並不會讓人們繼續前進。 – 2010-04-08 01:49:35

+0

除了我已經說過的內容外,這還不算太多。我想有人已經這樣做過,並不需要我顯示代碼,但它會是這樣的<%if(!Request.IsSecureConnection {%> show javascript here <% } %> – user204588 2010-04-08 02:00:18

+0

祝你好運。 – 2010-04-08 02:08:02

回答

1

這個工作對我來說:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <% if (!Request.IsSecureConnection) 
     { %> 
     <script type="text/javascript"> 
      onload = function() { 
         alert('Page is not secure') }; 
     </script> 
     <% } %> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

    </div> 
    </form> 
</body> 
</html> 
0
<%if (!Request.IsSecureConnection) 
{%> 
    <script ..........> </script> 
<%}%> 

這沒不工作?

更新從您的意見,這沒有奏效。我猜測它與你在代碼背後做的事情有關。你有沒有嘗試從你的代碼中調用RegisterClientScriptBlock?如果你可以發佈你的aspx和代碼,我們可能會幫助更多。

+0

沒有。我得到這個錯誤 類型'System.Web.HttpUnhandledException'的異常被拋出; Controls集合不能被修改,因爲控件包含代碼塊(即<% ... %>)。 – user204588 2010-04-08 02:34:29

+0

把這些東西放在代碼隱藏頁面而不是內聯標記。在你的腳本中包裹一個佔位符控件或類似的東西,並根據安全連接顯示/隱藏佔位符控件。 – 2010-04-08 03:34:53