访问DataBound项控件时出现不一致

时间:2022-04-29 15:35:18

Essentially, my question is this: There are two ways that I have experienced setting values to databound controls. Either this way:

基本上,我的问题是:我有两种方法可以设置数据绑定控件的值。这样:

<asp:Label runat="server" id="MyLabel"><%#DataBinder.Eval(Container.DataItem, "MyValue")%></asp:Label>

Or this way:

或者这样:

<asp:Label runat="server" id="MyLabel" text=<%#DataBinder.Eval(Container.DataItem, "MyValue")%> />

When trying to access items in an event handler (outside of the method that this databinding is occuring) using the first method, MyLabel.Text is an empty string. However, using the second way, MyLabel.Text will equal "MyValue". Can anyone tell me why this happens?

当尝试使用第一种方法访问事件处理程序中的项目(在此数据绑定发生的方法之外)时,MyLabel.Text是一个空字符串。但是,使用第二种方式,MyLabel.Text将等于“MyValue”。谁能告诉我为什么会这样?

2 个解决方案

#1


The Text property of a Label doesn't map to the inner text in the control markup. The Label control can be used as a container for other controls - so you'd put child controls inside the tag.

Label的Text属性不映射到控件标记中的内部文本。 Label控件可以用作其他控件的容器 - 因此您可以将子控件放在标签内。

The reason you're seeing the Text as empty when you bind using <%# ... %> is because the bound text is rendering as a child literal control in the MyLabel.Controls collection. In this case, you'd access the text as

使用<%#...%>进行绑定时,您看到Text为空的原因是因为绑定文本在MyLabel.Controls集合中呈现为子文字控件。在这种情况下,您可以访问文本

var myText = ((ITextControl)MyLabel.Controls[0]).Text;
// instead of..
var myText = MyLabel.Text;

If you want to access the Text of the label - always use the Text property. If you want to nest controls in your label - put them between the markup tags.

如果要访问标签的文本 - 请始终使用Text属性。如果要在标签中嵌套控件 - 将它们放在标记标记之间。

#2


Not sure about this but... It may be because in the second example, Text is a property of the Label control and you set it directly, whereas in the first example, you're not setting the Text property, you're just adding a child to the Label...

不确定这个但是......可能是因为在第二个例子中,Text是Label控件的属性并且你直接设置它,而在第一个例子中,你没有设置Text属性,你只是将一个孩子添加到Label ...

EDIT: A quick look with Reflector confirmed this: if the Label has some child content to it, it is that content that is rendered to html (but it's never set to the Text property). Otherwise, it is the content of the Text property that is rendered.

编辑:使用Reflector快速查看确认:如果Label有一些子内容,那就是呈现为html的内容(但它永远不会设置为Text属性)。否则,它是呈现的Text属性的内容。

#1


The Text property of a Label doesn't map to the inner text in the control markup. The Label control can be used as a container for other controls - so you'd put child controls inside the tag.

Label的Text属性不映射到控件标记中的内部文本。 Label控件可以用作其他控件的容器 - 因此您可以将子控件放在标签内。

The reason you're seeing the Text as empty when you bind using <%# ... %> is because the bound text is rendering as a child literal control in the MyLabel.Controls collection. In this case, you'd access the text as

使用<%#...%>进行绑定时,您看到Text为空的原因是因为绑定文本在MyLabel.Controls集合中呈现为子文字控件。在这种情况下,您可以访问文本

var myText = ((ITextControl)MyLabel.Controls[0]).Text;
// instead of..
var myText = MyLabel.Text;

If you want to access the Text of the label - always use the Text property. If you want to nest controls in your label - put them between the markup tags.

如果要访问标签的文本 - 请始终使用Text属性。如果要在标签中嵌套控件 - 将它们放在标记标记之间。

#2


Not sure about this but... It may be because in the second example, Text is a property of the Label control and you set it directly, whereas in the first example, you're not setting the Text property, you're just adding a child to the Label...

不确定这个但是......可能是因为在第二个例子中,Text是Label控件的属性并且你直接设置它,而在第一个例子中,你没有设置Text属性,你只是将一个孩子添加到Label ...

EDIT: A quick look with Reflector confirmed this: if the Label has some child content to it, it is that content that is rendered to html (but it's never set to the Text property). Otherwise, it is the content of the Text property that is rendered.

编辑:使用Reflector快速查看确认:如果Label有一些子内容,那就是呈现为html的内容(但它永远不会设置为Text属性)。否则,它是呈现的Text属性的内容。