What I’m trying to do is iterate through a repeater and read some controls values:
foreach (RepeaterItem iter in TablePanier.Items)
{
string guid = ((HiddenField)iter.FindControl("guid")).Value.ToString();
// nombre exemplaires du livre
int nbExemplaires = int.Parse(((System.Web.UI.WebControls.TextBox)iter.FindControl("txtNbExemplaires")).Text.ToString());
}
As you can see, I have a HiddenValue and a TextBox. Unfortunately this isn’t working, the values are not read correctly.
What’s wrong?
Thank you!
EDIT:
Here is the entire code of the form:
public partial class Panier : System.Web.UI.Page
{
Bussiness.Manager _manager = new Bussiness.Manager("MSSQLSERVER");
IEnumerable<Bussiness.iPanier> _paniers;
CurrencyConvertor _currencyConvertor = new CurrencyConvertor();
Bussiness.iCommande _commande;
int idPanier;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["login"] != null)
{
Security security = new Security();
_paniers = _manager.chargerPannierUtilisateur(Session["login"].ToString());
foreach (Bussiness.iPanier p in _paniers)
{
idPanier = p.id;
TablePanier.DataSource = p.Livres;
TablePanier.DataBind();
}
}
else
{
Response.Redirect("~/Accueil.aspx");
}
}
protected void btnCommande_Click(object sender, EventArgs e)
{
foreach (RepeaterItem iter in TablePanier.Items)
{
// id livre courant
if (iter.ItemType == ListItemType.Item || iter.ItemType == ListItemType.AlternatingItem)
{
string guid = ((HiddenField)iter.FindControl("guid")).Value.ToString();
int nbExemplaires = int.Parse(((System.Web.UI.WebControls.TextBox)iter.FindControl("txtNbExemplaires")).Text.ToString());
}
}
}
}
As you can see, the repeater is bound at the constructor level. And I’m trying to read the data when an event occur on a button of the page.
Any idea ?
A few things to check:
the repeater is bound in Page_Load on both the intial request and on postback. So when a button click posts the page, the repeater gets reloaded with the original data. Anything entered by the user is lost. Is this what you want? If not, use the IsPostBack property to load it on the GET request only.
if Session[“login”] is null, your repeater will not get loaded – I presume you have checked this?
in your posted code, you are not doing anything with the values you are trying to get from the repeater controls. Is this because there is additional code not shown?
Have you used the debugger to inspect the the controls found by FindControl to make sure they exist and they are what you think they are?
when you say ‘the values are not read”, do you mean you get null values? Empty strings?