如何在ASP.NET中的代码隐藏文件中访问服务器端控件? [英] How server side controls are accessible in code behind files in ASP.NET?

查看:70
本文介绍了如何在ASP.NET中的代码隐藏文件中访问服务器端控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好奇!



我想知道,究竟发生了什么,以便在代码中可以访问.aspx页面中定义的具有runat属性的任何控件在文件后面?



.aspx页面:

< asp:Label rundat =serverID = lbl/>



页面后面的代码:

lbl.Text =这是一个标签;



如果有人可以在这里分享一些关于实际发生的事情的信息,那么在添加runat属性后,我的标签可以在代码中被访问?



我尝试了什么:



我试图找到详细信息但只获取如何访问页面后面的代码中的控件而不是后面的实际机制。

解决方案

Quote:

在添加runat属性后,在代码后面可以访问我的标签是什么?





每当y在 ASPX 页面进行一些更改,设计器文件将在每次时更新runat =server属性被添加到控件中。

由于设计器文件是部分类,因此将在代码隐藏文件中访问受保护的成员。 />


例如
当你向页面添加一个asp按钮时


< asp :按钮ID =btnrunat =server/>设计器文件中的



,它将更新为

 protected global :: System.Web。 UI.WebControls.Button btn; 



因此可以在后面的代码中访问 btn 对象,因为代码隐藏和设计器文件充当部分类。



参考:

c# partial class [ ^ ]

部分类和方法(C#编程指南) [ ^ ]


Just curious!

I was wondering, what actually happens so that any control that is defined in .aspx page and having "runat" attribute is accessible in code behind file?

.aspx page:
<asp:Label rundat = "server" ID= "lbl" />

code behind page:
lbl.Text = "This is a label";

If anyone can share some information about what actually is happening here, what makes my label accessible in code behind after adding "runat" attribute?

What I have tried:

I've tried to find details but getting only how to access controls in code behind page not the actual machinism behind this.

解决方案

Quote:

what makes my label accessible in code behind after adding "runat" attribute?



Whenever you do some change in the ASPX page, the designer file will get updated everytime when the runat="server" attribute is added to the control.
Since the designer file is a partial class, the protected members will be accessed in the code behind file.

for example
when you add an asp button to the page

<asp:Button ID="btn" runat="server" />


in the designer file, it will update as

protected global::System.Web.UI.WebControls.Button btn;


and hence the btn object is accessible in the code behind, since code behind and designer file are acting as partial class.

refer:
c# partial class[^]
Partial Classes and Methods (C# Programming Guide)[^]


这篇关于如何在ASP.NET中的代码隐藏文件中访问服务器端控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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