ASP.net内联表达式问题 [英] ASP.net Inline Expression Issue

查看:137
本文介绍了ASP.net内联表达式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚为什么这不能在下面工作。我需要将文本框绑定到内联表达式中的值。好像一件简单的事情呢?但这些都不行。有任何想法吗?感谢提前。

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.

编辑:您还可以查看如何'绑定'标签中的Text属性这是一个难题。

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屋!

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