'txtName' is not declared. It may be inaccessible due to its protection level

后端 未结 13 1587
暖寄归人
暖寄归人 2020-12-15 19:22

After putting a textbox on my page when I compile it I get the above error:

\'txtName\' is not declared. It may be inaccessible due to its protection level.

13条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 19:59

    The way the classes are ordered is that your page's generated class inherits from your codebehind class - they're not partials, and it's not the other way around. This means that your codebehind class has no knowledge of the controls you've placed on the page. If you want access to a property that you declare in the page itself you need to declare it in the codebehind class first:

    protected TextBox txtName;
    

    You can then access this member in the codebehind file. This will get wired up correctly to match your control in the page.

提交回复
热议问题