重用ASP.NET code在多个页面 [英] Reusing ASP.NET code in multiple pages

查看:128
本文介绍了重用ASP.NET code在多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法重用一块ASP.NET code(即基于标签的code,而不是code-后面)在许多不同的页面?比如我有一个< D​​IV> 在它的一些内容出现在我的网站5不同的网页。我在寻找一种技术,通过它我可以拯救这个< D​​IV> 在一个单独的网页内容的文件,可以这么说,有可能不同的扩展名,比如 MyDiv.ASPC ,然后可以做一个服务器端的标签像网页的任意位置:

Is there a way to reuse a piece of ASP.NET code (that tag-based code, not the code-behind) in many different pages? For example I have a <div> with some contents in it that appears in 5 different pages of my site. I'm looking for a technique through which I could save this <div> in a separate web-content file so to speak, with maybe a different extension like MyDiv.ASPC and could then do a server-side tag anywhere in a webpage like:

<asp:Import href="~/MyDiv.aspc" />

和ASP.NET会在这一点上,其中此标记出现注入该文件的全部内容。

and ASP.NET would inject the entire content of this file at that point where this tag appears.

我想创建此用户的控制,但我不知道,如果一个用户控件始终注入precisely什么是写在它的身上,或者可能有有时用户控件本身产生不必要的标签。

I thought of creating a user control for this, but I'm not sure if a user control always injects precisely what is written in its body, or could there sometimes be unwanted tags generated by user control itself.

还是有存在这样做的更好的办法?

Or are there existing better ways of doing this?

修改

关于母版页,他们都远离了我要找的。他们实际上是很好的为您的网站的一个共同的基本布局。我的想法是截然相反的一点。我的网页没有一个共同的布局;这只是他们有一个共同的&LT; D​​IV&GT; 。它是由一个用户控件更紧密地应验了。

About Master Pages, they are far away from what I'm looking for. They are actually good for a common basic layout of your website. My idea is quite opposite of that. My pages do not have a common layout; it is just that they have one common <div>. It is more closely fulfilled by a UserControl.

有关UCS,我担心的是,他们产生比什么是写在自己的身体多了,而我后是一个简单的文本注入。把它从技术上来说,我要找的基本上是preprocessor步(在C种的#include东西++),而不是一个编译器的一步,如果你明白我的意思。

For UCs, my fear is that they generate more than what is written in their body, whereas what I'm after is a simple text injection. To put it technically, what I'm looking for is basically a preprocessor step (kind of #include thing in C++) rather than a compiler step, if you see what I mean.

推荐答案

您需要使用ASP.NET用户控件,因为这些是专门创造是解决你所描述的问题。欲了解更多信息,请参见 MS文档

You need to use ASP.NET User Controls, as these are specifically created to be the solution to the problem you are describing. For more information, see MS Documentation.

从他们的文档...

除了在你的ASP.NET网页使用Web服务器控件,
  你可以使用相同的创建自己的自定义,可重用控件
  你的技术用于创建ASP.NET网页。这些控件
  所谓的用户控件。

In addition to using Web server controls in your ASP.NET Web pages, you can create your own custom, reusable controls using the same techniques you use for creating ASP.NET Web pages. These controls are called user controls.

一个用户控件是一种复合控件中的作品很像一个
  ASP.NET网页,你可以添加现有的Web服务器控件和标记
  用户控件,并定义控件的属性和方法。
  然后,您可以将它们嵌入ASP.NET网页,在那里他们充当
  单位。

A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit.

这篇关于重用ASP.NET code在多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆