从Static方法访问控件 [英] Accessing controls from Static method

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

问题描述

在我的aspx页面中我有静态方法。

我无法从静态方法访问任何控件

如何访问该页面内的控件



在此先感谢,

In my aspx page i have static method.
I am not able to access any controls from static method
How can i access the controls inside that page

Thanks in advance,

推荐答案

看看simillar问题 - 答案线程链接。





http://stackoverflow.com/questions/952023/in-c-net-how-does-one-access-a-control-from-a-static-方法 [ ^ ]



http://stackoverflow.com/questions/4064906/how-to-update-controls-from-static-method [ ^ ]





http://stackoverflow.com/questions/595125/c-accessing-control- propertes-from-static-void [ ^ ]
Have a look at simillar Question-Answer thread links.


http://stackoverflow.com/questions/952023/in-c-net-how-does-one-access-a-control-from-a-static-method[^]

http://stackoverflow.com/questions/4064906/how-to-update-controls-from-static-method[^]


http://stackoverflow.com/questions/595125/c-accessing-control-propertes-from-static-void[^]


如何从非静态(实例)方法访问某些成员?从来没有关于它?这是可能的,因为具有这些实例成员的实例(对象)以 instance.method()的形式传递给调用。实际上, instance 引用作为隐式参数传递给 method()



你有提示吗?您只需要将类类型的显式参数添加到方法中并执行几乎相同的操作。但是,如果有实例方法,为什么要这样做呢?在一些相对罕见的情况下它可能是有用的。



-SA
How can you access some members from a non-static (instance) method? Never though about it? It it possible because the instance (object) having those instance members is passed to the call in the form instance.method(). In fact, instance reference is passed as an implicit parameter to method().

Are you getting a hint? All you need it to add an explicit parameter of the class type to the method and do nearly the same. But why doing it if there are instance methods? In some relatively rare cases it can be useful.

—SA


如果你正在使用Asp.net然后,您可以从静态函数访问页面控件(如asp.net访问控件)。



If you are using Asp.net then, You can access page controls (like asp.net access a control) from static function.

public static void Savedata()
{
    if (HttpContext.Current != null)
    {
        Page page = (Page)HttpContext.Current.Handler;
        TextBox TextBox1 = (TextBox)page.FindControl("TextBox1");

        TextBox TextBox2 = (TextBox)page.FindControl("TextBox2");
    }
}





以上方法用于查找控制值。 [WebMethod]的重点是它们不运行ASP.Net页面生命周期。这样,它们快速且可并行化。您的控件不存在。相反,你应该使用Javascript(更好)或UpdatePanel(更糟糕)。



Above method is for finding the control values. The whole point of [WebMethod]s is that they don't run the ASP.Net page lifecycle. This way, they're fast and parallelizable. Your controls don't exist. Instead, you should use Javascript (better) or an UpdatePanel (worse).


这篇关于从Static方法访问控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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