问题描述
如果它们不同,在什么情况下我们应该决定使用哪一种方法?
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 控件或复合控件,它是 Web 控件的特殊形式.这些控件没有设计器,通常在与您的 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 个有网格,其中三个网格是相同的.那么我能做的是为选项卡的内容创建一个单独的用户控件,这现在大大减少了我需要的代码(因为除了数据之外三个网格是相同的).
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 自定义控件与用户控件:这两个是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!