2009-06-29 65 views
2

我创建了一个公开我的后端对象模型的类库。我不想直接将数据绑定到SQL或XML,因为这里有大量的教程/演示似乎假设。ASP.Net和双向数据绑定

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

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

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

我已经阅读了数据绑定的几个概述,但没有什么似乎做我想做的 - 他们都谈论将数据集链接到SQL数据库!

谈谈StackOverflow的知识。

回答

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> 

编辑: 此外,(你可能已经意识到了这一点),你需要以某种方式继续存在列表/阵列。您可以一路回到数据库并在那里重新构建列表,或者根据您的需要将其存储在内存中,将缓存,视图状态或会话视为可行选项。

0

Spring.NET Web将为您提供双向数据绑定,具有漂亮,易于维护的代码。试一试!