【问题标题】:ASP.Net and two-way data bindingASP.Net 和双向数据绑定
【发布时间】:2009-06-29 16:16:05
【问题描述】:

我创建了一个公开我的后端对象模型的类库。我不希望直接将数据绑定到 SQL 或 XML,因为那里似乎有数量惊人的教程/演示。

在我的 Page_Load() 中,在 if (!IsPostbak) 中,我当前从对象模型中设置了我的控件的所有值,并在每个控件上调用 Databind()。

我有一个按钮事件处理程序,它从对象模型中删除一个项目(在这种情况下是一个列表)并将其重新绑定到中继器。首先,这很麻烦——每次都重新绑定——但更重要的是,页面重新加载时不会显示任何值。我应该将数据绑定代码放在 Page_Load() 中的 if 语句之外吗?

问题的第二部分是关于回归基础——在 Asp.net 中进行数据绑定的最佳方式是什么?我主要对绑定列表和数组感兴趣。我会想象有一种方法可以告诉控件(例如 TextBox)将数据绑定到字符串变量,并让字符串始终反映文本框的内容,并让文本框始终反映字符串的内容。我尝试了 语法,但没有比使用上述代码隐藏更进一步。

我已经阅读了几篇关于数据绑定的概述,但似乎没有什么能满足我的要求 - 他们都在谈论将 DataSet 链接到 SQL 数据库!

转向 StackOverflow 的知识。

【问题讨论】:

    标签: asp.net data-binding


    【解决方案1】:

    您需要在每次页面加载时进行数据绑定,因此您需要从 !IsPostBack 块中删除代码。

    当使用列表或数组处理控件上的数据绑定事件时。对于中继器,您需要处理 ItemDataBound 事件。

    此示例已从http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx修改:

    <%@ Page Language="C#" AutoEventWireup="True" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html  >
     <head>
        <title>OnItemDataBound Example</title>
    <script language="C#" runat="server">
           void Page_Load(Object Sender, EventArgs e) {
                 ArrayList values = new ArrayList();
    
                 values.Add(new Evaluation("Razor Wiper Blades", "Good"));
                 values.Add(new Evaluation("Shoe-So-Soft Softening Polish", "Poor"));
                 values.Add(new Evaluation("DynaSmile Dental Fixative", "Fair"));
    
                 Repeater1.DataSource = values;
                 Repeater1.DataBind();
           }
    
           void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
    
              // This event is raised for the header, the footer, separators, and items.
    
              // Execute the following logic for Items and Alternating Items.
              if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
    
                 if (((Evaluation)e.Item.DataItem).Rating == "Good") {
                    ((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
                 }
              }
           }    
    
           public class Evaluation {
    
              private string productid;
              private string rating;
    
              public Evaluation(string productid, string rating) {
                 this.productid = productid;
                 this.rating = rating;
              }
    
              public string ProductID {
                 get {
                    return productid;
                 }
              }
    
              public string Rating {
                 get {
                    return rating;
                 }
              }
           }
    
        </script>
    
     </head>
     <body>
    
        <h3>OnItemDataBound Example</h3>
    
        <form id="form1" runat="server">
    
           <br />
           <asp:Repeater id="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
              <HeaderTemplate>
                 <table border="1">
                    <tr>
                       <td><b>Product</b></td>
                       <td><b>Consumer Rating</b></td>
                    </tr>
              </HeaderTemplate>
    
              <ItemTemplate>
                 <tr>
                    <td> <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' Runat="server"/> </td>
                    <td> <asp:Label id="RatingLabel" Text='<%# DataBinder.Eval(Container.DataItem, "Rating") %>' Runat="server"/> </td>
                 </tr>
              </ItemTemplate>
    
              <FooterTemplate>
                 </table>
              </FooterTemplate>
    
           </asp:Repeater>
           <br />
    
        </form>
     </body>
     </html>
    

    编辑: 此外,(您可能已经意识到这一点)您需要以某种方式持久化您的列表/数组。您可以一直返回数据库并在那里重构列表,也可以根据需要将其与缓存、视图状态或会话一起作为可行的选项存储在内存中。

    【讨论】:

      【解决方案2】:

      Spring.NET Web 将为您提供双向数据绑定以及易于维护的代码。试试看!

      【讨论】:

        猜你喜欢
        • 2018-07-27
        • 1970-01-01
        • 2013-02-24
        • 2015-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        相关资源
        最近更新 更多