And*_*est 5 asp.net user-controls webforms designer visual-studio-2008
我创建了一个具有以下公共属性的UserControl:
[Browsable(true)]
public string Text
{
get { return pnlLookupTable.GroupingText; }
set { pnlLookupTable.GroupingText = value; }
}
Run Code Online (Sandbox Code Playgroud)
正如您可能已经猜到的那样,pnlLookupTable是一个Panel控件.我可以在"属性"窗口中更改Text属性的值,它会像标记一样反映在标记中.但是,页面内UserControl的设计视图不会显示Panel的更新GroupingText.我怎么能让这件事发生?
编辑:
根据请求,这是该属性所属的整个类.你可以看到没有什么特别的事情:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class LookupTable : System.Web.UI.UserControl
{
[Browsable(true)]
public string Text
{
get { return pnlLookupTable.GroupingText; }
set { pnlLookupTable.GroupingText = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
这是.ascx文件的相关部分:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LookupTable.ascx.cs" Inherits="LookupTable" %>
<asp:Panel ID="pnlLookupTable" runat="server" GroupingText="Lookup Table">
<%-- Irrelevant content here. --%>
</asp:Panel>
Run Code Online (Sandbox Code Playgroud)
我已经更改了一些标识符和其他琐碎的东西来保护代码的专有性质.
我还应该重申,我正在设计模式中查看添加了控件的Web表单,并且我正在更改Text属性.我想在设计器中直观地看到Panel的GroupingText的变化.
我必须回顾一下UserControlDesigner代码......
简短的故事:我不认为这是可能的.
这是长篇故事:
From what I recall, User Controls located in ASCX files are never run in the designer. That is, the code inside the ASCX or ASCX.CS file is never compiled or run in Visual Studio. This is to prevent memory leaks caused by the fact that in the CLR you cannot unload assemblies that you have loaded. In order to run the code in your User Control, Visual Studio would have to compile your ASCX into a DLL, then load it, and then run the code. Every time you make a change to the ASCX, it would have to perform this operation again. Every time this operation happens more memory will be consumed by the additionally loaded DLL generated from your ASCX.
由于CLR中的这种限制,用户控件设计器实际上并不编译或运行ASCX文件.相反,它解析ASCX文件并在其中查找控件,而是加载这些控件.对于它在ASCX文件中找到的每个控件,它将创建关联的控件设计器并呈现该控件的设计时HTML.
有几种方法可以解决这个问题:
这两个解决方案应该工作的原因是它们都涉及将代码编译成DLL.这个想法是DLL不会经常更改,因此Visual Studio加载DLL是安全的,而不必在每次DLL更改(和泄漏内存)时重新加载它.
| 归档时间: |
|
| 查看次数: |
4816 次 |
| 最近记录: |