473,383 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

Hello,

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)
Nov 19 '05 #1
1 2277
>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[i].FindControl("hidVarID")).Value);
nPVar.varName = ((TextBox)rptNpVars.Items[i].FindControl("txtNpVarName")).ToString();
nPVar.showVar = ((CheckBox)rptNpVars.Items[i].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;
arrNpVars.Add(nPVar);
}
// 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;
arrNpVars.Add(newNpVar);
// so now we have the array of eCommVariation objects set up, sort it and bind it to the repeater
arrNpVars.Sort();
rptNpVars.DataSource = arrNpVars;
rptNpVars.DataBind();

The ItemTemplate of the repeater looks like this...

<ItemTemplate>
<tr>
<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>
<td align="left" valign="top">
<asp:TextBox ID="txtNpVarName" Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20" MaxLength="20" cssClass="box"
RunAt="server" />
</td>
<td align="center" valign="top">
<asp:CheckBox ID="chkShowNpVar" Text="" Checked='<%#TrueIfY((string)DataBinder.Eval(Contai ner.DataItem, "show_avalue"))%>' RunAt="server"/>
</td>
</tr>
</ItemTemplate>

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[i].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(Contai ner.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)
Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark Fox | last post by:
Hello, I have a repeater and in each itemtemplate I have a radiobuttonlist. I am attempting to figure out how on postback I could iterate through the rows displayed by the repeater and for...
1
by: Pums | last post by:
Hi All For one page I want to implement sortng as well as paging. Curretly that page is using datagrid for data binding and sorting. Datagrid supports attributes like sortexpression and sort order...
7
by: Scott Schluer | last post by:
Hi All, I have a functioning datagrid on "Page 1" that displays order information for a single order (this is for an e-commerce site). It's actually a combination of a couple datagrids to...
4
by: Ryan Ternier | last post by:
Thanks for the previous help guys! I got my list box issue working, but now i'm trying to loop through all the items in my page. I want to find each listbox, once I do i strip the ID down to...
2
by: Antoine | last post by:
I would like to construct my own list of items in a grid/ table/ item list layout but I have a problem. I want to add a sort of index row based on time, such as there might be blank values. Sure...
3
by: WebMatrix | last post by:
I am struggling with implementing somewhat complicated UI web-control. I explored Repeater, but I am not sure if it's the best way to go. I am leaning towards writing my own custom control and...
3
by: Andrew | last post by:
Hi, I am working on a questionnaire. I have displayed a questionnaire using a repeater control. The itemtemplate is as below (quite cut down): <ItemTemplate> <tr><td> <%#...
2
by: HockeyFan | last post by:
Yesterday, I posted a question dealing with an issue of trying to reference (from javascript on the client side) an item within a Repeater. My code was hard-coded to use the actual ClientId, but...
4
by: adiel_g | last post by:
I am trying to loop through a repeater to retrieve a dataitem field but am getting a NullReferenceException. I can find a checkbox control but cannot find a dataitem field. Here is the code that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.