从一个单独的类访问页面控件 [英] Accessing page controls from a seperate class
问题描述
我不知道我怎么能去从一个单独的类我做访问页面控件。我已经尝试了一些事情,我发现使用谷歌,但没有运气:(
I'm wondering how I can go about accessing page controls from a separate class I've made. I've tried a few things I found using google, but no luck :(
我想要做的就是保持这种经常使用的功能,在全球一流的。
What I'm trying to do is keep a function that is used often, in a global class.
该函数然后访问一个页面的文字,并呼吁ScriptManager.RegisterStartupScript。我希望这是可能的,这样的话这个功能就不会被复制到所有的页面。
The function then accesses a page literal and calls ScriptManager.RegisterStartupScript. I was hoping this is possible, so then this function wouldn't have to be copied to all of the pages.
任何意见将大大AP preciated。
Any ideas would be greatly appreciated.
推荐答案
我不知道我是否正确地理解你 - 如果不是,张贴一些示例code
I'm not sure whether I understood you correctly - if not, post some sample code.
如果你有你的页面的前违约和背后的code是这样的:
If you have your page ex Default and the code behind like this:
public partial class Default : System.Web.UI.Page
{
//Some methods
}
您可以做一个辅助类是这样的:
You can make a helper class like this:
class Helpers
{
public static void YourHelperMethod(Default page)
{
//You can access your controls here like:
page.someLabelControl.Text = "Some text";
}
}
如果你想让它与所有页面,则可以找到类似下面的控制。你应该,但是,使用母版页和code背后是否有需要在所有的页面做了什么。
And if you want it to work with all pages, you can "find" the control like the following. You should, however, use Master pages and the code behind if there is something that need to be done on all pages.
class Helpers
{
public static void YourHelperMethod(Page page)
{
//You can access your controls here like:
Label label = page.FindControl("labelName") as Label;
if (label == null)
//The control could not be found
label.Text = "Some text";
}
}
这篇关于从一个单独的类访问页面控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!