简体   繁体   English

在.aspx页面上引用代码隐藏属性?

[英]Referencing code-behind properties on .aspx page?

On a .aspx page, what is the best way to link a server control's property to a property of the page class (its code-behind)? 在.aspx页面上,将服务器控件的属性链接到页面类的属性(其代码隐藏)的最佳方法是什么? The only way that I have read about is to use databinding: 我读过的唯一方法是使用数据绑定:

<asp:TextBox ID="txt" runat="server" Text='<%# Me.SomePropOfMine %>' />

and then call Me.txt.DataBind() or Me.Databind() from the codebehind. 然后从代码隐藏中调用Me.txt.DataBind()Me.Databind() Is there any way of establishing this relationship on the .aspx page alone, or simplifying the process if you have many controls to bind (without binding the entire page)? 有没有办法单独在.aspx页面上建立这种关系,或者如果你有许多控件要绑定(不绑定整个页面),简化过程?

You can Databind() entire Me or a container control (you can add a PlaceHolder control around your desired controls also). 您可以使用Databind()整个Me或容器控件(您也可以在所需的控件周围添加PlaceHolder控件)。 because DataBind() goes recursively on Child controls. 因为DataBind()在Child控件上递归递归。

A better approach if you don't need DataBinding except for this is to use Code Expression Binder 如果您不需要DataBinding,则更好的方法是使用Code Expression Binder

http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

This allows you to use <%$ Code: Me.Property %> instead of <%# Me.Property %> . 这允许您使用<%$ Code: Me.Property %>而不是<%# Me.Property %>

For more about expression builders in general if you don't know them at all check this intro post: http://www.4guysfromrolla.com/articles/022509-1.aspx 有关表达式构建器的更多信息,如果您根本不了解它们,请查看此简介: http//www.4guysfromrolla.com/articles/022509-1.aspx

Note that <%= Me.Property %> will NOT work on web controls like <asp:TextBox ... and such... 请注意, <%= Me.Property %>用于像<asp:TextBox ...这样的Web控件......

PS PS

The only drawback with Code expression builder is that you get no intellisense. Code表达式构建器的唯一缺点是您没有智能感知。 I usually work around this by writing <%= Me.TestSomething %> inside the markup to get my intellisense, and then replace <%= with <%$ Code: when done. 我通常通过在标记内写<%= Me.TestSomething %>来获取我的intellisense,然后将<%=替换为<%$ Code:完成后解决此问题。 Annoying, but if you don't want to go the DataBind() route (and you shouldn't cause it may conflict with existing real data binding you want to do. Trust me, trying to make those work is hell), then this is the way to go. 烦人,但如果你不想去DataBind()路线(你不应该导致它可能与你想要做的现有真实数据绑定冲突。相信我,试图让这些工作是地狱),然后这是要走的路。

There are two things: 有两件事:

  1. Use <%= instead of <%# if you want to read the values of some variables. 如果要读取某些变量的值,请使用<%=而不是<%#。
  2. You can use Page.DataBind() to bind all the controls in the page. 您可以使用Page.DataBind()绑定页面中的所有控件。

If I would ever actually need to do this, I would use CodeExpressionBuilder . 如果我真的需要这样做,我会使用CodeExpressionBuilder

But question is - why do you want to set properties in markup and not in code behind? 但问题是 - 为什么要在标记中设置属性而不是在代码后面? If they are dynamic and related to logic (and probably they are), then you should set them in code behind even if that looks unconvenient - that would keep logic in one place and markup in other. 如果它们是动态的并且与逻辑相关(并且可能是它们),那么你应该在代码中设置它们,即使这看起来不方便 - 这会将逻辑保留在一个地方而标记在其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM