views:

433

answers:

3

Hi,

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 ?

+2  A: 

Check the item type - you should be looking for or an item or alternating item type.

if ( iter.ItemType == ListItemType.Item || iter.ItemType = ListItemType.AlternatingItem ) {
// find controls and do something
}
ScottE
+2  A: 

Depends on where you have the controls.

Please note that the repeater has a HeaderItemTemplate, ItemTemplate & AlternateItemTemplate & FooterItemTemplate.

Due to this, when you are iterating through the Item collection of the repeater, you need to be aware of which of the templates you are looking for before getting to the object of the control.

Typically you would need to code like:

foreach(RepeaterItem item in TablePanier.Items){

 if(item.ItemType == ListItemType.HeaderItem){
  // do something with the header
 }
 else if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem){
  // do something with the item
    var guid_control = item.FindControl("guid") as HiddenField;
    var nbExemplaires = item.FindControl("txtNbExemplaires") as TextBox;
 }
 else if(item.ItemType == ListItemType.FooterItem){
  // do something with the footer
 }

HTH. }

Sunny
+2  A: 

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?

Ray
Thank you! The first point solved my problem!
Amokrane