【发布时间】:2014-10-21 11:39:42
【问题描述】:
我正在尝试实现 TextBox 到页面属性的最简单的可能(在我看来)数据绑定,除了数据绑定应该是双向的,所以我使用 @ 而不是 <%# Test %> 987654322@。 (实际上,目标是将单个对象作为属性并绑定到它的属性,但让我们从简单的事情开始。)我正在用这个简单的代码对其进行测试:
TestForm.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TestTextBox" runat="server" Text='<%# Bind("Test") %>' />
</div>
</form>
</body>
</html>
TestForm.aspx.cs:
using System;
namespace WebApplication1
{
public partial class TestForm : System.Web.UI.Page
{
public string Test { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Test = "Hello";
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
DataBind();
}
}
}
DataBind() 的调用导致InvalidOperationException:Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。
这种方法有什么问题?
【问题讨论】:
标签: c# asp.net .net data-binding webforms