ASP.net 内联表达式问题 [英] ASP.net Inline Expression Issue
问题描述
我似乎无法弄清楚为什么这在下面不起作用.我需要将文本框绑定到内联表达式中的值.看起来是件很简单的事情吧?但这些都不起作用.有任何想法吗?提前致谢.
I can't seem to figure out why this does not work below. I need to bind the text box to a value from an inline expression. Seems like a simple thing right? But neither of these work. Any ideas? Thanks in advance.
<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
<asp:textbox id="tbName" runat="server" Text='<%= "test" %>' />
我应该提到,这个页面没有背后的代码,只有顶部的以下指令.
I should mention that this page has no code behind and only the following directives at the top.
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Page Language="C#" %>
我能想到的唯一可行的解决方案是添加一个内联服务器脚本,就像这个一样.我希望我知道为什么除非您处于数据绑定上下文中,否则内联表达式将不起作用.
The only workable solution that I could come up with short of adding a code behind is adding an inline server script, like this one. I wish I knew why the inline expressions won't work unless you're in a data binding context.
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
tbName.Text = "test";
}
</script>
推荐答案
在 Page_Load 中你将不得不为
In the Page_Load of you will have to make a call to Page.DataBind() for
<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
上班.
<%= %> 是缩短的 response.Write() 并且对于任何服务器标记永远不会作为属性有效.
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.
<%# %> 可以使用,仅当容器是数据绑定的(在您的情况下为页面).
<%# %> can be used, only if the conatainer is databound (the page in your case).
<%$ %> 可用于访问资源文件中的数据.
<%$ %> can be used to access data in resources files.
您还可以查看 如何在标记中绑定"标签的文本属性 这是一个类似的问题.
You can also take a look at How to 'bind' Text property of a label in markup which is a smimilar question.
这篇关于ASP.net 内联表达式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!