A
Alan Silver
Hello,
I'm trying to use an ArrayList to do data binding, but am getting an
error I don't understand. I posted this in another thread, but that was
all confused with various other problems, which could be why no-one
answered it!! I've now narrowed the problem down, so am starting a new,
more specific, thread. Sorry it's a bit long, but it's reasonably simple
stuff.
I have a page where I'm showing product details, including any
non-priced variations available (say it comes in red, green and blue,
all at the same price), as well as any priced variations (say it comes
in small, medium and large, each at a different price).
I want to show the variation details in two repeaters, with all values
in editable controls. That way the user can edit as many as they like
without having to postback for each one.
I also have a set of controls where they can enter details for a new
variation. They click a link button and this creates an ArrayList
containing the variations from the existing repeater, plus the new one.
I then want to display this new list in the repeater. This is where I
get stuck ;-(
I have created a class for the variations as follows...
public class eCommVariation : IComparable {
public int avalueid; // the database ID
public string varName; // the displayed name (red, green, etc)
public bool showVar; // will this variation be shown?
public float price; // price (if a priced variation)
public bool specialOffer; // is it on special offer (if a priced
variation)
public float specialPrice; // special price (if a priced variation)
// method for IComparable so I can use the class for data binding
public int CompareTo(object objRhs) {
eCommVariation rhs = (eCommVariation)objRhs;
return String.Compare(this.varName, rhs.varName);
}
}
In the page itself, I pull the product details from a database when the
page first loads and display them. When the user clicks the "add new
variation" link button, the following code is called...
void lbtAddNpVar_Click(object o, EventArgs e) {
// add a new non-priced variation
// first set up the array of eCommVariation objects that will be used for databinding
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(((Literal)rptNpVars.Items.FindControl("hidVarID")).Text);
nPVar.varName = ((TextBox)rptNpVars.Items.FindControl("txtNpVarName")).Text;
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;
arrNpVars.Add(nPVar);
}
// now add the new value
eCommVariation newNpVar = new eCommVariation();
newNpVar.avalueid = 0; // shows it's a new value
newNpVar.varName = txtNewNpVar.Text;
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();
}
(this code is for the non-priced variations, so the three class members
relevant to priced ones are just set to dummy values)
The problem is when the DataBind() method is called. The ItemTemplate of
the repeater looks like this (HTML simplified)...
<ItemTemplate>
<asp:CheckBox ID="chkDeleteNpVar" Text="" Checked="False" RunAt="server"/>
<br><asp:TextBox ID="txtNpVarName" Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20" MaxLength="20" cssClass="box"
RunAt="server" />
<br><span style="color:#ffffff"><asp:Literal ID="hidVarID" Text='<%#DataBinder.Eval(Container.DataItem, "avalueid")%>' RunAt="server" /></span>
<br><asp:CheckBox ID="chkShowNpVar" Text="" Checked='<%#TrueIfY((string)DataBinder.Eval(Container.DataItem, "show_avalue"))%>'
RunAt="server"/>
</ItemTemplate>
When it tries to do the data binding, it gives an error
"DataBinder.Eval: 'PixataWebUtils.eCommVariation' does not contain a
property with the name avalue" on the line...
<br><asp:TextBox ID="txtNpVarName"
Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20"
MaxLength="20" cssClass="box" RunAt="server" />
Now I know the class *does* contain a property (well, actually a public
variable, but I think that amounts to the same thing doesn't it?) with
the name "avalue". If I swap the order of the controls in the
ItemTemplate, then it gives the error on whichever comes first, so it's
not actually that the property doesn't exist, it's that it doesn't like
*any* of them.
What am I doing wrong here? It seems such a simple, basic thing that I'm
stuck. I know you can do data binding with an ArrayList, but I must be
missing some basic point here.
Any suggestions welcome. TIA.
I'm trying to use an ArrayList to do data binding, but am getting an
error I don't understand. I posted this in another thread, but that was
all confused with various other problems, which could be why no-one
answered it!! I've now narrowed the problem down, so am starting a new,
more specific, thread. Sorry it's a bit long, but it's reasonably simple
stuff.
I have a page where I'm showing product details, including any
non-priced variations available (say it comes in red, green and blue,
all at the same price), as well as any priced variations (say it comes
in small, medium and large, each at a different price).
I want to show the variation details in two repeaters, with all values
in editable controls. That way the user can edit as many as they like
without having to postback for each one.
I also have a set of controls where they can enter details for a new
variation. They click a link button and this creates an ArrayList
containing the variations from the existing repeater, plus the new one.
I then want to display this new list in the repeater. This is where I
get stuck ;-(
I have created a class for the variations as follows...
public class eCommVariation : IComparable {
public int avalueid; // the database ID
public string varName; // the displayed name (red, green, etc)
public bool showVar; // will this variation be shown?
public float price; // price (if a priced variation)
public bool specialOffer; // is it on special offer (if a priced
variation)
public float specialPrice; // special price (if a priced variation)
// method for IComparable so I can use the class for data binding
public int CompareTo(object objRhs) {
eCommVariation rhs = (eCommVariation)objRhs;
return String.Compare(this.varName, rhs.varName);
}
}
In the page itself, I pull the product details from a database when the
page first loads and display them. When the user clicks the "add new
variation" link button, the following code is called...
void lbtAddNpVar_Click(object o, EventArgs e) {
// add a new non-priced variation
// first set up the array of eCommVariation objects that will be used for databinding
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(((Literal)rptNpVars.Items.FindControl("hidVarID")).Text);
nPVar.varName = ((TextBox)rptNpVars.Items.FindControl("txtNpVarName")).Text;
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;
arrNpVars.Add(nPVar);
}
// now add the new value
eCommVariation newNpVar = new eCommVariation();
newNpVar.avalueid = 0; // shows it's a new value
newNpVar.varName = txtNewNpVar.Text;
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();
}
(this code is for the non-priced variations, so the three class members
relevant to priced ones are just set to dummy values)
The problem is when the DataBind() method is called. The ItemTemplate of
the repeater looks like this (HTML simplified)...
<ItemTemplate>
<asp:CheckBox ID="chkDeleteNpVar" Text="" Checked="False" RunAt="server"/>
<br><asp:TextBox ID="txtNpVarName" Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20" MaxLength="20" cssClass="box"
RunAt="server" />
<br><span style="color:#ffffff"><asp:Literal ID="hidVarID" Text='<%#DataBinder.Eval(Container.DataItem, "avalueid")%>' RunAt="server" /></span>
<br><asp:CheckBox ID="chkShowNpVar" Text="" Checked='<%#TrueIfY((string)DataBinder.Eval(Container.DataItem, "show_avalue"))%>'
RunAt="server"/>
</ItemTemplate>
When it tries to do the data binding, it gives an error
"DataBinder.Eval: 'PixataWebUtils.eCommVariation' does not contain a
property with the name avalue" on the line...
<br><asp:TextBox ID="txtNpVarName"
Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20"
MaxLength="20" cssClass="box" RunAt="server" />
Now I know the class *does* contain a property (well, actually a public
variable, but I think that amounts to the same thing doesn't it?) with
the name "avalue". If I swap the order of the controls in the
ItemTemplate, then it gives the error on whichever comes first, so it's
not actually that the property doesn't exist, it's that it doesn't like
*any* of them.
What am I doing wrong here? It seems such a simple, basic thing that I'm
stuck. I know you can do data binding with an ArrayList, but I must be
missing some basic point here.
Any suggestions welcome. TIA.