从页面基类查找在页面控件 [英] Finding a Control in a page from a page base class

查看:342
本文介绍了从页面基类查找在页面控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望你有一个良好的周五和东西......好吧,所以这里是我的问题:

Hope you're having a good Friday and stuff... okay, so here's my question:

我所有的ASPX页面从所谓的BasePage基类继承。从的BasePage继承:

All my ASPX pages inherit from a base class called BasePage. BasePage inherits from:

System.Web.UI.Page

现在,我怎么访问/从我的页面基地设置控件在我的aspx页面?我试过这样:

Now, how do I access/set a control in my aspx page from my page base? I've tried this:

HyperLink hypMainMenu = (HyperLink)Page.FindControl("hypMainMenu");
hypMainMenu.NavigateUrl = AppConfiguration.AppSettings.Urls.MainMenu;

但hypMainMenu总是空 - 我找不到这个混蛋。有任何想法吗?或者这是不好的做法,有人可以reccommend一个更好的方式来做到这一点?

But hypMainMenu is always null - I can't find the bastard. Any ideas? Or is this bad practice and someone can reccommend a better way to do this?

在此先感谢!

推荐答案

虽然是什么,是不是好的做法是非常主观的,我必须说,试图访问的从控制任何从它的基类是不好的做法。基类应该的包含code是相关的所有类,将继承它,你怎么知道,每次你在未来写的页面将有这种控制?

Even though what is and is not good practice is very subjective, I must say that trying to access anything from a control from it's base class is bad practice. A base class should only contain code that is relevant for all classes that will inherit it, and how do you know that every page you write in the future will have this control?

有一个更好的办法来解决你的问题(我假设是具有你的主菜单中的只有一个地方code)是写在用户控件和你包括在每一页上。如果你需要做一些具体的事情与控制,可以在 UC做 codeBehind

A better way to solve your problem (which I assume to be having code for your main menu in only one place) would be to write a UserControl and which you include on every page. If you need to do something specific with that control, you can do it in the UC's CodeBehind.

这篇关于从页面基类查找在页面控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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