ASP.Net自定义控件与用户控件:这些是两个相同的 [英] ASP.Net Custom controls vs. user controls: Are these two the same
问题描述
如果他们是不同的,我们应该决定使用这两种方法在什么情况下?
If they are different, under what circumstances should we decide to use either approach?
此外,什么是ASCX的优势ASPX?
Also, what is the advantage of ascx over aspx?
推荐答案
用户控件是自定义的控制的一种形式,是给你一个可视化设计。当你想在同一网站内重复使用的控制他们是理想之选。 (它可以创建和包装用户控件作为单独的组件,但是这超出了这个问题的范围,我知道已被要求在SO)。
user controls are a form of custom control, that gives you a visual designer. They are ideal for use when you want a reusable control within the same web site. (It is possible to create and package user controls as seperate assemblies but that is beyond the scope of this question and I know has been asked on SO).
一个自定义的控制通常用来指一个Web控件,或复合控件它是一个网络控制的特殊形式。这些控件没有设计师,通常在允许从您的网络单独的项目,他们翻过许多网站被重用实现。
A custom control is typically used to refer to a Web Control, or a Composite Control which is specialized form of a web control. These controls have no designer and are usually implemented in seperate projects from your web allowing them to be reused accross many sites.
现在你的第二个问题,ASCX和ASPX是两回事。 ASCX是用户控件,那里的ASPX是一个ASP.Net页面的扩展。你不能单独使用ASCX必须将其放置到一个ASPX或母版页。
Now your second question, ASCX and ASPX are two different things. ASCX is the extension for a User Control, where as ASPX is an ASP.Net Page. You cannot use an ASCX by itself it must be placed onto an ASPX or Master page.
我喜欢使用的用户控件的方法之一是我有例如一个非常复杂的网页,其中有7个标签,那些有5格,网格那些他们三人都是identicle的。那么有什么我能做的就是创造的标签内容的一个单独的用户控件,现在这个降低了code,我需要向下显著(自三格栅是identicle除了数据)。
One way I like to use user controls is I have for example a very complex page which have 7 tabs, 5 of those have grids, of those grids three of them are identicle. Well what I can do is create a seperate user control for the content of the tabs, this now reduces the code I need down significantly (Since three grids are identicle except for the data).
更进一步,它允许多个,在页面的各个部分的工作,并且它可以帮助我把一切都直,因为我减少了页面的复杂程度。你不使用的用户控件,而不是网页,你一起选择使用他们的页面。
Further more it allows multiple to work on various parts of the page, and it helps me keep everything straight since I am reducing the complexity of the page. You do not use User Controls instead of Pages, you use them in conjuction with a page.
您不要使用超过ASCX ASPX。您可以使用ASCX来补充。例如在我的网站每一页都有相同的页脚,但我不希望每个页面从一个母版页派生。我可以创建页脚为acsx控制并把它放在我的每个母版页。
You do not use ascx over aspx. You use ascx to complement. For example on my site every page has the same footer, but I don't want every page to derive from a single master page. I can create my footer as an acsx control and put it in each of my master pages.
另外一个例子,我有一个表单,允许用户输入三种不同的日期范围。 (我们有其他的形式)。于是,我就把逻辑,使压延按钮,一个文本框,点击时打开了压光机,在用户的控制。那么我可以重用在我所有的aspx页面的用户控件。
Another example, I have a form that lets a user enter three different date ranges. (And we have other forms). So I put the logic to enable a calender button, and a text box that when clicked on opens up the calender, in a user control. I can then reuse that user control in all my aspx pages.
这篇关于ASP.Net自定义控件与用户控件:这些是两个相同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!