ASP.NET 中的两种方式数据绑定
假设我们有一个对象,
class Entity
{
public string ID {get; set;}
public string Name {get; set;}
}
我想将属性绑定到页面上的两个文本框,如下所示:
<asp:FormView ID="FormView" runat="server">
<ItemTemplate>
<asp:textbox ID="TextId" Text='<%# Bind("ID") %>'/>
<asp:textbox ID="TextId" Text='<%# Bind("Name") %>'/>
</ItemTemplate>
</asp:FormView>
然后在后面的代码中编写此
public EntityObject
{
get { return ViewState["Entity"] as Entity; }
set { ViewState["Entity"] = value; }
}
protected override void OnInit(EventArgs e)
{
if (EntityObject== null)
EntityObject= new EntityObject();
FormView.DataSource = new[] { EntityObject };
FormView.DataBind();
base.OnInit(e);
}
内容当我在文本框中输入值时,我希望 EntityObject 在回发后重新加载页面时在属性中包含这些值,但属性始终为空。
Let say that we have an object
class Entity
{
public string ID {get; set;}
public string Name {get; set;}
}
I want to bind properties to two textboxes on a page something like this:
<asp:FormView ID="FormView" runat="server">
<ItemTemplate>
<asp:textbox ID="TextId" Text='<%# Bind("ID") %>'/>
<asp:textbox ID="TextId" Text='<%# Bind("Name") %>'/>
</ItemTemplate>
</asp:FormView>
and then write this in code behind
public EntityObject
{
get { return ViewState["Entity"] as Entity; }
set { ViewState["Entity"] = value; }
}
protected override void OnInit(EventArgs e)
{
if (EntityObject== null)
EntityObject= new EntityObject();
FormView.DataSource = new[] { EntityObject };
FormView.DataBind();
base.OnInit(e);
}
And when I enter values in textboxes I expect EntityObject to have these values in properties when page reloads after PostBack, but properties are always null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
遗憾的是,ASP.NET 不支持与 .net 对象的双向绑定...
相反,您可以在每次回发时使用类似“手动绑定”的内容(此处 AddIncomeSources 是 RepeaterControl)
Sadly to say that, but ASP.NET does not support two-way binding to .net objects...
instead you can use something like "manual binding" on every post back (here AddIncomeSources is RepeaterControl)
在 OnInit 中,您总是将其设为 null.. 删除这些代码并保留在页面加载中。.
只需要在第一次绑定,而不是在所有回发中绑定。
页面加载()
{
}
In the OnInit you are always making to null.. remove those code and keep in the page load..
only need to bind in the very first time not in all the post back.
Page_Load()
{
}
这里的问题是你正在尝试创建动态控制。
你可以在.aspx页面中使用gridview控件并绑定数据,它默认具有viewstate而不是你的。
无论如何web是无状态的,我们应该通过ViewState或Session或Application保留数据。您也可以将其保留在缓存中,但不可靠(我们可以使其实用可靠)。大多数 .NET 控件都内置了 ViewState。
here the problem is you are trying to create dynamic control.
you can use a gridview control in .aspx page and bind the data, it has viewstate by default instead of yours..
Anyway web is stateless, we should retain the data through ViewState or Session or Application. Also you can keep it in cache but not reliable (we can make it pragmatically reliable). Most of the .NET controls has ViewState in-built.
对于那些寻找的人,我制作了这个 javascript 插件。它的数据绑定功能与 WPF 的功能非常接近,并且易于使用。它在 .txt 文件中附加了文档
https://github.com/jdemeuse1204/ObjectDataBinding
这是一个我的绑定之一的示例 激活
您调用
获取对象以进行保存或其他操作
For those looking, I made this javascript plugin. It does data binding very close to what WPF does and is easy to use. It has attached documentation in a .txt file
https://github.com/jdemeuse1204/ObjectDataBinding
Here is an example of what one of my bindings looks like
To activate you call
To get the object back for saving or other operations
我为此创建了一个解决方案。
https://www. codeproject.com/Articles/146078/WPF-Two-way-Databinding-in-ASP-NET-Enabling-MVVM
或 github:
https://github.com/Recodify/mvvm-4-asp-webforms
I created a solution for this.
https://www.codeproject.com/Articles/146078/WPF-Two-way-Databinding-in-ASP-NET-Enabling-MVVM
or github:
https://github.com/Recodify/mvvm-4-asp-webforms