文章編號: 911721 - 上次校閱: 2006年3月13日 - 版次: 1.1

可能無法在執行階段存取公用屬性,而且您可能會收到錯誤訊息在 ASP.NET 2.0 架構的應用程式

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

徵狀

考慮下列案例。在 Microsoft ASP.NET 2.0 架構的應用程式,Web 網頁會包含 Microsoft Windows Form 使用者控制項物件。您可以使用 JavaScript 嘗試存取這個 Windows Form 使用者控制項物件的屬性。在這種情況下您可能無法在執行階段存取公用屬性。此外,您可能會收到下列的例外狀況錯誤訊息:
請參閱這封郵件而不是此對話方塊如需叫用剛的時間 (JIT) 偵錯的詳細資訊的結尾。
************** 例外狀況文字 **************
為型別的使用權限要求 System.Security.SecurityException: 'System.Security.permissions.SecurityPermission Mscorlib,版本 = 2.0.0.0 文化特性 = 中性 PublicKeyToken = b77a5c561934e089' 失敗。 at System.Windows.Forms.SafeNativeMethods.IFont.GetHFont() at System.Windows.Forms.Control.ActiveXImpl.get_AmbientFont() at System.Windows.Forms.Control.get_Font() at System.Windows.Forms.Control.GetParentFont() at System.Windows.Forms.Control.get_Font() at System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.CommonLayout() at System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintWorker(PaintEventArgs e, Boolean up, CheckState state) at System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintUp(PaintEventArgs e, CheckState state) at System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintOver(PaintEventArgs e, CheckState state) at System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.Paint(PaintEventArgs pevent) at System.Windows.Forms.ButtonBase.OnPaint(PaintEventArgs pevent) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
已失敗的動作:
要求
第一個失敗的使用權限的型別為: System.Security.Permissions.SecurityPermission
************** 載入的組件 **************
mscorlib
組件版本: 2.0.0.0
Win32 版本: 2.0.50215.44 (beta2.050215 4400)
程式碼基底: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50215/mscorlib.dll
----------------------------------------
系統
組件版本: 2.0.0.0
Win32 版本: 2.0.50215.44 (beta2.050215 4400)
程式碼基底: file:///C:/WINDOWS/assembly/GAC_MSIL/system/2.0.0.0__b77a5c561934e089/system.dll
----------------------------------------
SmartClient
組件版本: 1.0.1928.26893
Win32 版本: 無
程式碼基底: http://localhost:1156/SmartClientTestWeb/SmartClient.dll
----------------------------------------
System.Windows.Forms
組件版本: 2.0.0.0
Win32 版本: 2.0.50215.44 (beta2.050215 4400)
程式碼基底: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
組件版本: 2.0.0.0
Win32 版本: 2.0.50215.44 (beta2.050215 4400)
程式碼基底: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
Microsoft.mshtml
組件版本: 7.0.3300.0
Win32 版本: 7.0.3300.0
程式碼基底: file:///C:/WINDOWS/assembly/GAC/Microsoft.mshtml/7.0.3300.0__b03f5f7f11d50a3a/Microsoft.mshtml.dll
----------------------------------------
************** JIT 偵錯 **************
若要啟用剛的時間 (JIT) 偵錯,.config 檔案,此應用程式或電腦 (machine.config) 必須有將 system.windows.forms] 區段中設定 jitDebugging 值。應用程式也必須啟用偵錯編譯。
例如:
<configuration>< system.windows.forms jitDebugging ="true"/ ></configuration>
啟用 JIT 偵錯時任何未處理的例外狀況將會被送到 JIT 偵錯工具在電腦上註冊而非由這個對話方塊。
附註這個問題不會發生在 Microsoft.NET Framework 1.1。

發生的原因

使用者控制項 的基底類別包含下列屬性,就會發生這個問題。
ComVisible(false)

其他可行方案

如果要解決這個問題,將下列屬性加入至 使用者控制項 類別。
ComVisible(true)

其他相關資訊

如果要重現問題的步驟

  1. 啟動 Microsoft Visual Studio 2005。
  2. 在 [檔案] 功能表上按一下 [新網站]。
  3. 在 [範本] 清單按一下 [ASP.NET 網站]。
  4. 在 [位置] 方塊中,輸入 DriveLetter:\Path\KB911721
  5. 在 [語言] 清單按一下 [Visual C#,再按 [確定]
  6. 在 [方案總管] 中 Default.aspx,] 上按一下滑鼠右鍵,然後按一下 [開啟舊檔]。
  7. 下列程式碼取代現有的程式碼。
    <%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <script language=javascript>
            function TestData()
            {
                alert('Test1');
                form1.TestControl.TestProperty = "gomusin";
                alert(form1.TestControl.TestProperty);
            }
            
            function TestData1()
            {
                alert('Test2');
                var sData = form1.TestControl.TestProperty;
                alert(sData);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <object id="TestControl" classid="SmartClient.dll#SmartClient.UserControl1" VIEWASTEXT>
            </object>
            <input id="Button2" type="button" value="Property Read" style="width: 165px" onclick="javascript:TestData1()" />
            <input id="Button1" type="button" value="Property Write" style="width: 168px" onclick="javascript:TestData()"/>
    </div>
        </form>
    </body>
    </html>
  8. 檔案] 功能表上按一下 [新增],然後按一下 [新增專案]。
  9. 在 [範本] 清單按一下 Windows 控制項程式庫
  10. 在 [名稱] 方塊中,輸入 SmartClient
  11. 在 [位置] 方塊中輸入 DriveLetter:\Path,],然後再按一下 [確定]]。
  12. 在 UserControl1.Designer.cs 檔案的程式碼設計工具中, 現有的程式碼取代下列程式碼。
    namespace SmartClient
    {
        partial class UserControl1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Component Designer generated code
    
            /// <summary>
            /// Required method for designer support - do not modify 
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.button2 = new System.Windows.Forms.Button();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(7, 90);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(126, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "property Read";
                this.button1.Click += new System.EventHandler(button1_Click);
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(146, 90);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(127, 23);
                this.button2.TabIndex = 1;
                this.button2.Text = "property Write";
                this.button2.Click += new System.EventHandler(button2_Click);
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(22, 25);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(232, 21);
                this.textBox1.TabIndex = 2;
                // 
                // UserControl1
                // 
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.button1);
                this.Name = "UserControl1";
                this.Size = new System.Drawing.Size(276, 123);
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.TextBox textBox1;
        }
    }
  13. 在 [方案總管] 中 UserControl1.cs,] 上按一下滑鼠右鍵,然後按一下 [檢視程式碼
  14. 下列程式碼取代現有的應用程式程式碼。
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SmartClient
    {
      public partial class UserControl1 : UserControl
      {
        public UserControl1()
        {
          InitializeComponent();
        }
    
        String TestProperty = "something";
    
        void button2_Click(object sender, System.EventArgs e)
        {
          textBox1.Text = TestProperty;
        }
    
        void button1_Click(object sender, System.EventArgs e)
        {
          TestProperty = textBox1.Text;
        }
      }
    }
  15. 在 [偵錯] 功能表上按一下 [開始偵錯]。

    附註在您按一下任何] 按鈕之後您會發現 < 徵狀 > 一節所述的問題。
在本文所討論的協力廠商產品是由 Microsoft 以外的公司所製造的。Microsoft 可讓不以暗示或其他方式,效能或可靠性這些產品的保證。

這篇文章中的資訊適用於:
  • Microsoft ASP.NET 2.0
關鍵字:?
kbmt kbtshoot kbprb KB911721 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:911721? (http://support.microsoft.com/kb/911721/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
Retired KB Article依現狀不再更新的知識庫內容免責聲明
本文旨在說明 Microsoft 不再提供支援的產品。因此,本文係依「現狀」提供,不會再更新。