我们如何在控件初始化之前检索控件的属性 [英] How we able to retrieve the properties of controls before their are initialized

查看:85
本文介绍了我们如何在控件初始化之前检索控件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_PreInit(object sender, EventArgs e)
        {
//If i have a textbox with id t1, then eventhogh it is not initialized in this event(Untill Init event) then how am able to accessing the properties here
string text=t1.Text;

        }
 protected void Page_Init(object sender, EventArgs e)
        {
string text=t1.Text; //It should possible
        }

推荐答案

我会认为上述文本框是在设计时拖放到"页面上,而不是在运行时创建的? ?如果是这样,那么使用全局引用可以在页面上的任何位置访问该控件是完全正常的.如果未设置text属性,则只会在PreInit事件中获得空白文本.如果查看xxx.aspx.designer.cs文件,则会看到在网页上创建新控件时,其全局引用已添加到设计器文件中,您可以使用该全局引用访问后面的代码中的任何位置的控件.文件.

如果您在Page_Init事件中声明了控件,则无法从Page_PreInit中访问t1,因为它已超出上下文或范围.

希望这会有所帮助.

干杯...
I would imagine that the said text box was "dragged-n-dropped" onto the page at design time and not created at runtime?? if thats the case, then its perfectly normal to be able to access that control anywhere on the page using the global reference. You will only get blank text in the PreInit event if the text property hasn''t been set. If you look at the xxx.aspx.designer.cs file, you would see that when you create a new control on the web page, its global reference is added in the designer file using which you can access the control anywhere in the code behind file.

If you had declared the control in the Page_Init event then t1 wouldn''t be accessible from Page_PreInit because it would have been out of context or scope.

Hope this helps.

Cheers...


PreInit 阶段,控制值将始终为null.如果您想使用某种检查或计数...请改用global veriable.
PreInit 用于在初始化控件之前设置页面级属性.例如页面的主题或标题.
at the stage of PreInit, control value will always be null. if you would like to use some sort of check or count...use global veriable instead.
PreInit is to set the page level properties before initializing its controls. like theme or title of the page.


这篇关于我们如何在控件初始化之前检索控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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