禁用母版页上的按钮.net 4.0 (c#)
当满足某些条件时,我试图从内容页面禁用主页上的按钮。 我可以使用会话变量更改母版页按钮上的文本,但即时通讯在使用最新的这一点时遇到了一些麻烦 - 有人有任何提示吗?
Im trying to disable a button on the masterpage from a content page when certain criteria are met.
I can change the text on masterpage buttons using session variables, but IM having a bit of trouble with this latest bit - any hints anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的母版页上,您将有一个公共功能,如下所示:
在您的内容页面上,您将编写:
On your Master Page you would have a public function like so:
On your content page you would write:
我会向您的母版页添加一个公共方法来执行此操作,并让内容页调用它。这可以使事情变得更加容易并且更加面向对象。
您可以在内容页中使用
MasterType
指令对母版页进行强类型化。这将使调用您的方法变得更加容易。I would add a public method to your master page that does this, and let the content page call it. This can make things a lot easier and more object-oriented.
You can use the
MasterType
directive in the content page to strongly type the master page. This will make it easier to call your method.这是很多人都遇到的常见问题,并且有一种非常“简单”的方法可以使其发挥作用。但是,如果您计划让该应用程序不断增长或变得非常复杂,那么采取简单的方法就会对您造成伤害。大多数其他答案都会告诉您简单的方法。 我们至少讨论正确的方法。
首先,我们要明确母版页和内容页之间的关系。内容页面不应直接调用其主页面上特定于实现的成员,反之亦然。它们由 Page-Master 合约分开。在逻辑层面上发生了什么?内容页面上发生的某些情况应导致特定按钮被禁用。
现在,由于按钮位于母版页上,因此它不应该知道特定内容页。因此,按钮也不应该负责调用页面实例并检查条件,也不应该显式订阅特定于类型的事件。
那么该怎么办呢?应该有一些主数据对象(可能是单例)在整个请求生命周期中驻留在集中可用的位置。例如,
HttpContext.Items
或Session
。作为主数据对象,它应该是管理您正在谈论的这些条件的单一权限。当任何数据发生更改时,它应该引发一个事件,并且任何人(例如您的按钮)都可以订阅该事件并根据状态做出决策。 (示例如下)如果您的应用程序非常简单,那么这绝对是矫枉过正。但是,随着您作为开发人员的成长,并开始构建更复杂的系统,您需要牢记这些事项,以使您的应用程序更简单、更稳定且更易于维护。
这是该方法解决的两个具体的、现实世界的问题,它们来自我的经验。
这个非常简单 - 如果您需要在运行时交换母版页,您希望将页面和母版页实现完全解耦。一个母版页可能有另一个母版页没有的东西。我的一个具体案例有点极端 - 我们有数十个母版页嵌套达 6 层深,在运行时将它们换入换出。但灵活性的原则仍然是一样的 - 看看这个网站上有多少关于如何在运行时更改母版页的问题!
这个有点令人兴奋,但仍然具有非常实际的后果。将逻辑职责分解为单独的类有一个非常重要的效果 - 它通常会减少应用程序中逻辑分支(例如
if
语句)的总数。这些是错误的最大来源。如果一个类没有任何逻辑,它就很难有错误! :) 当您开始从人年和开发人员的角度来考虑应用程序,并以数十万或数百万行代码来衡量时,整个应用程序中聚合的逻辑分支较少会对稳定性产生真正的、可衡量的影响和可变性。维护和增强更多地是安排简单、稳定的组件,而不是进行代码手术。这是主数据类的示例:
This is a common problem that comes up from many people, and there is a really "easy" way to make it work. However, if you plan on this application growing or being significantly complex, taking the easy way out will hurt you. Most of the other answers are going to tell you about the easy way. Let's at least discuss the right way.
First, let's be clear on the relationship between master and content pages. A content page should not directly invoke implementation-specific members on its master, or vice versa. They are separated by the Page-Master contract. What's happening at a logical level? Something is happening on the content page that should result in a specific button being disabled.
Now, since the button is on the master page, it should not be aware of the specific content page. So it should also not be the button's responsibility to call down to the page instance and check for the criteria, nor should it explicitly subscribe to a type-specific event.
So what to do? There should be some master data object (probably a singleton) that lives in a centrally available location across the request lifetime. Say,
HttpContext.Items
orSession
. Being a master data object, it should be the single authority for managing these conditions you're talking about. It should raise an event when any of the data is changed, and anyone (such as your button) can subscribe to that event and make decisions based on the state. (Sample below)If your app will be stupid-simple, then this is definitely overkill. But as you grow as a developer, and start to build more sophisticated systems, these are things you need to keep in mind to keep your apps simpler, more stable, and more maintainable.
Here's two concrete, real-world things this approach addresses, that come from my experience.
This one's pretty straightforward - if you need to swap out masterpages at runtime, you want to decouple your page and masterpage implementations completely. One masterpage might have things another doesn't. One specific case of mine is a bit extreme - we have dozens of masterpages nested up to 6 layers deep, swapping them in and out at runtime. But the principle of flexibility remains the same - look at how many questions there are on this site for how to change the masterpage at runtime!
This one's a little more heady, but still has very practical consequences. Breaking logical responsibilities into separate classes has one very important effect - it typically reduces the total number of logic branches (e.g.
if
statements) in your applications. Those are the single biggest source of bugs. If a class doesn't have any logic, it's pretty hard for it to have bugs! :) When you start to think about applications in terms of man-years and development staff over time, and measure in hundreds of thousands or millions of lines of code - fewer logic branches aggregated over the whole application has a real, measurable impact on stability and changeability. Maintenance and enhancements become more about arranging simple, stable components than doing code surgery.Here's the example masterdata class: