c# - TextBox 中的 ASP.net 简写

标签 c# asp.net webforms response.write shorthand

我正在尝试执行以下操作:

<asp:TextBox ID="txtName" runat="server" Text="<%= Name %>" />

当我执行页面时,它会输出为<%= Name %>,而不是实际执行response.write。

我尝试修改它以使用 <% Response.Write(Name) %> 但它做了同样的事情,而是将文本放在那里。

我可以很好地做到这一点:

<input type="text" value="<%= Name %>" />

这确实有效。为什么当我使用 TextBox 控件时这不起作用?我还有其他方法可以做到这一点吗?

最佳答案

使用隐藏代码:

txtName.Text = Name;

或者,添加Page.DataBind()在后面的代码中并将控件的语法更改为:

<asp:TextBox ID="txtName" runat="server" Text="<%# Name %>" />

请注意 #而不是=#表示数据绑定(bind)表达式

关于c# - TextBox 中的 ASP.net 简写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6203257/

相关文章:

c# - 哪一个是更好的架构/设计方法?

c# - 拥有兼容的接口(interface),无需继承

c# - OWIN ApiController 访问请求正文/字符串化 JSON

c# - 我无法使用 Visual Studio 2019 中的 Google Cloud Platform 工具将 App Engine 应用程序发布到 GCloud

c# - 从 Silverlight 控件中获取数据

c# - linq & distinct,实现 equals 和 gethashcode

c# - 在 WPF 中使用系统图标作为应用程序图标

asp.net页面定位问题

c# - 页面正在加载但未渲染 style.css

jquery - 如何在 ASP.NET Web 表单中实际使用 JQuery Validation 远程验证