Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

You may not be able to access the public property at run time, and you may receive an error message in an ASP.NET 2.0-based application


View products that this article applies to.

Symptoms

Consider the following scenario. In a Microsoft ASP.NET 2.0-based application, a Web page contains a Microsoft Windows Forms user control object. You use JavaScript to try to access the properties of this Windows Forms user control object. In this scenario, you may not be able to access the public property at run time. Additionally, you may receive the following exception error message:
See the end of this message, instead of this dialog box, for details on invoking just-in-time (JIT) debugging.
************** Exception Text **************
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. 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)
The action that failed was:
Demand
The type of the first permission that failed was: System.Security.Permissions.SecurityPermission
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50215.44 (beta2.050215-4400)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50215/mscorlib.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50215.44 (beta2.050215-4400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
SmartClient
Assembly Version: 1.0.1928.26893
Win32 Version: n/a
CodeBase: http://localhost:1156/SmartClientTestWeb/SmartClient.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50215.44 (beta2.050215-4400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50215.44 (beta2.050215-4400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
Microsoft.mshtml
Assembly Version: 7.0.3300.0
Win32 Version: 7.0.3300.0
CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.mshtml/7.0.3300.0__b03f5f7f11d50a3a/Microsoft.mshtml.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this application or computer (machine.config) must have the jitDebugging value set in the system.windows.forms section. The application must also be compiled with debugging enabled.
For example:
<configuration> <system.windows.forms jitDebugging="true" /> </configuration>
When JIT debugging is enabled, any unhandled exception will be sent to the JIT debugger registered on the computer rather than be handled by this dialog box.
Note This problem does not occur in the Microsoft .NET Framework 1.1.

↑ Back to the top


Cause

This problem occurs because the UserControl base class contains the following attribute.
ComVisible(false)

↑ Back to the top


Workaround

To work around this problem, add the following attribute to the UserControl class.
ComVisible(true)

↑ Back to the top


More information

Steps to reproduce the problem

  1. Start Microsoft Visual Studio 2005.
  2. On the File menu, click New Web Site.
  3. In the Templates list, click ASP.NET Web Site.
  4. In the Location box, type DriveLetter:\Path\KB911721.
  5. In the Language list, click Visual C#, and then click OK.
  6. In Solution Explorer, right-click Default.aspx, and then click Open.
  7. Replace the existing code with the following code.
    <%@ 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. On File menu, click Add, and then click New Project.
  9. In the Templates list, click Windows Control Library.
  10. In the Name box, type SmartClient.
  11. In the Location box, type DriveLetter:\Path, and then click OK.
  12. In the code designer for the UserControl1.Designer.cs file, replace the existing code with the following code.
    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. In Solution Explorer, right-click UserControl1.cs, and then click View Code.
  14. Replace the existing application code with the following code.
    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. On the Debug menu, click Start Debugging.

    Note After you click any button, you notice the problem that is mentioned in the "Symptoms" section.
The third-party products that this article discusses are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, regarding the performance or reliability of these products.

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB911721, kbprb, kbtshoot

↑ Back to the top

Article Info
Article ID : 911721
Revision : 2
Created on : 3/13/2006
Published on : 3/13/2006
Exists online : False
Views : 303