我正在尝试将TextBox的数据绑定实现到页面的属性(在我看来,这是最简单的方法),除了数据绑定应该是双向的,因此<%# Test %>
我使用代替<%# Bind("Test") %>
。(实际上,目标是将单个对象作为属性并绑定到其属性,但让我们从简单的事情开始。)我正在以下简单的代码上对其进行测试:
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())只能在数据绑定控件的上下文中使用。
这种方法有什么问题?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句