views:

764

answers:

2

I've created a class library which exposes my back-end object model. I don't wish to databind straight to SQL or XML, as a surprising number of tutorials/demos out there seem to assume.

In my Page_Load(), within the if (!IsPostbak), I currently set all the values of my controls from the object model, and call Databind() on each control.

I have a button event handler which deletes an item from the object model (it's a List in this case) and rebinds it to the Repeater. First of all, this is messy - rebinding each time - but more importantly, no values are displayed when the page reloads. Should I put the databinding code outside the if statement in Page_Load()?

The second part of the question is regarding going back to basics - what's the best way to databind in Asp.net? I'm mainly interested in binding against lists and arrays. I would have imagined there being a way to tell a control (e.g. TextBox) to databind to a string variable, and for the string to always reflect the contents of the text box, and the text box to always reflect the contents of the string. I tried the <%#...%> syntax but got no further than using the code-behind as described above.

I've read several overviews of databinding, but nothing out there seems to do what I want - they all talk about linking DataSets to a SQL database!

Turning to the knowledge of StackOverflow.

A: 

You'll need to databind on every page load so you need to remove the code from within your !IsPostBack block.

When using a list or array handle the databinding event on the control. For a repeater you'll need to handle the ItemDataBound event.

This example has been modified from 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"&gt;
<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>

EDIT: In addition, (and you may already be aware of this) you'll need to persist your list/array somehow. You can go all the way back to the database and reconstitute the list there or you can store it in memory with cache, viewstate, or session as viable options depending on your needs.

ddc0660
A: 

Spring.NET Web will provide you with two-way databinding with nice, easy to maintain code. Give it a try!

Ben