How do I add items to a repeater whilst keeping what's already there?

Discussion in 'ASP .Net' started by Alan Silver, Jul 6, 2005.

  1. Alan Silver

    Alan Silver Guest


    I have a page in which I'm trying to give the user the chance to
    manipulate a list of items. These are the price variations for a
    product, so each item consists of a name (eg, small, medium, large), a
    price, a checkbox to say whether or not the variation is on special
    offer and a special price.

    I can pull the values out of the database OK and display them in a
    repeater. What I want to do is have a set of controls below the repeater
    that allows them to add a new variation. They would fill in the name,
    price, etc and click the button. The new variation would be added to the
    list and shown in alphabetical order with the other variations.

    Not too hard so far, except that I don't want to write anything back to
    the database at this stage. I want to keep this all on the page until
    they have finished and only then update the database. This is where I
    got stuck ;-)

    The problem is that in PageLoad I pull the values out of the database
    and bung them in the repeater. Subsequent postbacks will just use that
    original data. I need a way of keeping the modified data (ie any changed
    values, new variations and deleted variations) and displaying those.

    I had thought about creating some sort of array or datareader containing
    the latest set of values (pulled out of the repeater on postback), then
    making any changes, such as removing deleted variations, then adding in
    the new variation, then sorting the array and finally binding it to the
    repeater. Sounds easy eh? Well I haven't a clue as to how I would do
    it!! I'm stuck just deciding which kind of array (or similar) I would
    use to store the info, as well as how to store it.

    Any idea how I would go about this? Any and all suggestions appreciated.

    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Jul 6, 2005
    1. Advertisements

  2. Alan Silver

    Alan Silver Guest

    >Any idea how I would go about this? Any and all suggestions appreciated.

    Well, since posting I have made some progress, but I'm not sure if I'm
    doing this the most sensible way. For that matter, what I'm doing
    doesn't work anyway!! I will post what I have so far and would be very
    grateful for any comments. Sorry it's a bit long, but it's not too hard

    First I created a new class...

    public class eCommVariation : IComparable {
    public int avalueid;
    public string varName;
    public bool showVar;
    public float price;
    public bool specialOffer;
    public float specialPrice;

    public int CompareTo(object objRhs) {
    eCommVariation rhs = (eCommVariation)objRhs;
    return String.Compare(this.varName, rhs.varName);

    and used this on postback to create an ArrayList containing the existing
    values in the repeater (which is called rptNpVars), add the new value,
    sort and then bind the repeater to this ArrayList...

    ArrayList arrNpVars = new ArrayList();
    // add the existing variations
    for (int i=0; i<rptNpVars.Items.Count; i++) {
    eCommVariation nPVar = new eCommVariation();
    nPVar.avalueid = Convert.ToInt32(((HtmlInputHidden)rptNpVars.Items.FindControl("hidVarID")).Value);
    nPVar.varName = ((TextBox)rptNpVars.Items.FindControl("txtNpVarName")).ToString();
    nPVar.showVar = ((CheckBox)rptNpVars.Items.FindControl("chkShowNpVar")).Checked;
    // next three aren't relevant for non-priced variations, set them to dummy values
    nPVar.price = 0;
    nPVar.specialOffer = false;
    nPVar.specialPrice = 0;
    // now add the new value
    eCommVariation newNpVar = new eCommVariation();
    newNpVar.avalueid = 0; // shows it's a new value
    newNpVar.varName = txtNewNpVar.ToString();
    newNpVar.showVar = chkShowNewNpVar.Checked;
    // set the next three to anything as before
    newNpVar.price = 0;
    newNpVar.specialOffer = false;
    newNpVar.specialPrice = 0;
    // so now we have the array of eCommVariation objects set up, sort it and bind it to the repeater
    rptNpVars.DataSource = arrNpVars;

    The ItemTemplate of the repeater looks like this...

    <td align="center" valign="top">
    <asp:CheckBox ID="chkDeleteNpVar" Text="" Checked="False" RunAt="server"/>
    <input type="hidden" name="hidVarID" value='<%#DataBinder.Eval(Container.DataItem, "avalueid")%>' RunAt="Server" />
    <td align="left" valign="top">
    <asp:TextBox ID="txtNpVarName" Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20" MaxLength="20" cssClass="box"
    RunAt="server" />
    <td align="center" valign="top">
    <asp:CheckBox ID="chkShowNpVar" Text="" Checked='<%#TrueIfY((string)DataBinder.Eval(Container.DataItem, "show_avalue"))%>' RunAt="server"/>

    Now, I have two problems here. The first is that the line where I try to
    set nPVar.avalueid for the existing item fails. Here is the line...

    nPVar.avalueid = Convert.ToInt32(((HtmlInputHidden)rptNpVars.Items.FindControl("hidVarID")).Value);

    This gives an error "Object reference not set to an instance of an
    object". I tried splitting this into two lines, one to get the
    HtmlInputHidden control, and one to pull out the value, but this only
    showed me that the control could not be found. Any idea why when it is
    obviously (to me!!) in the ItemTemplate?

    I tried commenting that line out and setting the id to zero whilst I
    tested the rest, and then I got this error...

    DataBinder.Eval: 'PixataWebUtils.eCommVariation' does not contain a
    property with the name avalueid.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: DataBinder.Eval:
    'PixataWebUtils.eCommVariation' does not contain a property with the
    name avalueid.

    Source Error:

    Line 218:<ItemTemplate>
    Line 219:<tr>
    Line 220:<td align="center" valign="top"><asp:CheckBox ID="chkDeleteNpVar" Text="" Checked="False" RunAt="server"/><input type="hidden"
    name="hidVarID" value='<%#DataBinder.Eval(Container.DataItem, "avalueid")%>' RunAt="Server" /></td>
    Line 221:<td align="left" valign="top"><asp:TextBox ID="txtNpVarName" Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20"
    MaxLength="20" cssClass="box" RunAt="server" /></td>Line 222:<td align="center" valign="top"><asp:CheckBox ID="chkShowNpVar" Text=""
    Checked='<%#TrueIfY((string)DataBinder.Eval(Container.DataItem, "show_avalue"))%>' RunAt="server"/></td>

    Source File: D:\WebSites\E-CommercePackage\pdap\product.aspx Line: 220

    Now you can see from the code earlier that the eCommVariation class
    *does* contain a property called "avalueid". I have deleted and
    recompiled the DLL, but it doesn't make any difference.

    I'm sure these are two stupid mistakes, but I really can't see where
    they are. Any help would be greatly appreciated. TIA

    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Jul 6, 2005
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?R2FyeQ==?=
    Wasi Rehman
    Oct 29, 2004
  2. =?Utf-8?B?Q2hyaXM=?=

    Keeping listbox items without resetting them each time

    =?Utf-8?B?Q2hyaXM=?=, Feb 13, 2005, in forum: ASP .Net
    Feb 13, 2005
  3. Alan Silver
    Alan Silver
    Aug 4, 2005
  4. Replies:
  5. Anjan Bhowmik
    Misbah Arefin
    Feb 14, 2008
  6. Andy B.
    Andy B.
    Feb 19, 2009
  7. Replies:
  8. Replies:
    Steven D'Aprano
    Nov 20, 2012