且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在page_load中使用master.findcontrol

更新时间:2023-02-26 19:50:53

1.检查是否在page指令中添加了mastertype 声明.
2.如果要使用set/get textbox 值,则不要执行findcontrol,而是在masterpage 中定义一个公共属性以进行设置并获取它,然后从页面中调用此属性.
1. Check whether you have added the mastertype declaration in the page directive.
2. if you want to use set/get the textbox value then instead of doing a findcontrol define a public property in the masterpage for setting and getting it and then call this property from the page.


我的第一个猜测是您的控件实际上不在母版页上,而是在嵌套页上.如果是这样,将找不到它.您将需要执行以下操作:
My first guess would be that your control is not actually on the master page but on a nested page. If so, this won''t find it. You would need to do something like:
this.Form.FindControl("txtEmployee_code");



您可以在此处找到更多示例和更多想法(请务必查看评论,因为它们还有其他人尝试过的选项):
http://csharporcas.blogspot.com/2006/05/master-page-find- control.html [^ ]



You can find more examples and more ideas here (make sure to look at the comments, since they have other options people have tried):
http://csharporcas.blogspot.com/2006/05/master-page-find-control.html[^]


尝试TextBox tb =(TextBox)Page.FindControl("txtEmployee_code");
try TextBox tb = (TextBox)Page.FindControl("txtEmployee_code");