How to display in a form the next object of a collection?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • espaces70
    New Member
    • Jun 2010
    • 2

    #1

    How to display in a form the next object of a collection?

    I have a list of objects produced.
    after click on next I want to display the next product in the form

    in my asp page I have the following form:

    Code:
    <form id="formNF" runat="server">
        
        <asp:TextBox ID="txtRef" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtArticle" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtQauntity" runat="server"></asp:TextBox>
        
        
        <asp:Button ID="ButtonNext" runat="server" Text="Next product" OnClick="ButtonNext_Click"/>
        </form>
    In my code-behind:

    Code:
     private int i; 
        protected void ButtonNext_Click(object sender, EventArgs e) { 
             int nbrproducts = command.getListeproducts().Count; 
             i++; 
             for(i=0;i<nbrproducts;) { 
               mdeTxtRef.Text = command.getListeproducts()[i].Ref; 
               mdeTxtQte.Text = command.getListeproducts()[i].Qte; 
             } 
        }
    But it seems as ASP doesn't like this. Have u got an idea? thk u.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Store "i" into Session, or ViewState, or ControlState so that the page can retrieve what the last index was that the user was viewing.

    For example:
    Code:
    private int i;
    
    protected void Page_Load(object sender, EventArgs e){
      if(IsPostBack == true){
        i = (int)ViewState["productIndex"];
      }else{
        i = 0;
      }
    }
    
    void Page_PreRender(object sender, EventArgs e){
      ViewState["productIndex"] = i;
    }
    
    protected void ButtonNext_Click(object sender, EventArgs e) { 
      int nbrproducts = command.getListeproducts().Count; 
      i++; 
      //not sure why you're looping at this point...
      //you know the index..just retrieve the item.
      //for(i=0;i<nbrproducts;) { 
    
      if(i<getListeproducts.Length()){
        //not sure what command is either....
        mdeTxtRef.Text = command.getListeproducts()[i].Ref; 
        mdeTxtQte.Text = command.getListeproducts()[i].Qte; 
      }
      //} 
    }

    Comment

    • espaces70
      New Member
      • Jun 2010
      • 2

      #3
      Thank you for this answer, I stored "i" in textbox.
      but your solution seem better, I will try with viewstate.
      thanks.

      Comment

      Working...