简体   繁体   English

继承ASP.NET的代码

[英]Inheriting code for ASP.NET

I'm using ASP.NET web forms in Visual Studio 2010. How can I create UI controls and such programmatically? 我在Visual Studio 2010中使用ASP.NET Web表单。如何以编程方式创建UI控件? For example, I want to define in separate form files to have a side-bar menu and such, and then the resulting pages will inherit that functionality. 例如,我想在单独的表单文件中定义一个侧面菜单等,然后结果页面将继承该功能。 In my base class, I added a new control to this.Controls , but generating the page throws an exception, saying that the control must be placed inside a tag with runat="server" . 在我的基类中,我向this.Controls添加了一个新控件,但是生成页面会引发异常,表示必须将该控件放置在带有runat="server"的标记内。 If I want to generate these controls programmatically, how do I place them inside such a tag? 如果我想以编程方式生成这些控件,如何将它们放在这样的标记中?

You write: 你写:

and then the resulting pages will inherit that functionality 然后结果页面将继承该功能

which makes me believe you are better off using Master Pages, here's a tutorial on ASP.NET master pages (use Google to find more). 这使我相信您最好使用母版页,这是有关ASP.NET母版页的教程 (使用Google查找更多信息)。 However, inheritance itself can also easily be achieved by taking any existing control and extending it: 但是,继承本身也可以通过采用任何现有控件并将其扩展来轻松实现:

class MyTextBox : TextBox
{
    // your extensions and overrides
}

Later you write: 稍后您写:

I added a new control to this.Controls, but generating the page throws an exception, saying that the control must be placed inside a tag with runat="server". 我向this.Controls添加了一个新控件,但是生成页面会引发异常,说该控件必须放在带有runat =“ server”的标记内。

which is not entirely related. 这并不完全相关。 This is simply true: every server control must have a runat="server" _ or it won't be recognized by the ASP.NET parser. 这是完全正确的:每个服务器控件都必须具有runat="server" _,否则ASP.NET解析器将无法识别它。 Ie, like this: 即,像这样:

<!-- existing server control, placed on a page -->
<asp:Label runat="server" Id="MyOwnLabel" />
<!-- your own server control, placed on a page -->
<my:MyTextBox runat="server" ID="ReallyMyTextBox" />

I'm a tad bit confused. 我有点困惑。 It sounds like you're trying to add elements across various pages programmatically. 听起来您正在尝试以编程方式在各个页面上添加元素。 In ASP.NET this isn't required. 在ASP.NET中,这不是必需的。

I believe what you are looking for (and what would be the right tool for the job) would be ASP.NET Master Pages . 我相信您正在寻找的ASP.NET Master Pages (以及最适合该工作的工具)。

Master Pages will allow you to define a kind of template for your pages with common content (like navigation, ad bars, etc.). 母版页可让您为具有共同内容(如导航,广告栏等)的页面定义一种模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM