Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. loop for store Id in a list

loop for store Id in a list

Scheduled Pinned Locked Moved ASP.NET
css
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    strawberrysh
    wrote on last edited by
    #1

    I wrote this code for getting the id of every row in gridview when a button click.I don't know what code should i write for loop(because maybe a user click on several button to order a product).I want to when a button click the Id of it store in a list.

    protected void grid_product(object sender, GridViewCommandEventArgs e)
    {
    string currentcommand = e.CommandName;
    int currentrowindex = Int32.Parse(e.CommandArgument);
    string productid = GridView1.DataKeys[currentrowindex].Value;
    }

    R 1 Reply Last reply
    0
    • S strawberrysh

      I wrote this code for getting the id of every row in gridview when a button click.I don't know what code should i write for loop(because maybe a user click on several button to order a product).I want to when a button click the Id of it store in a list.

      protected void grid_product(object sender, GridViewCommandEventArgs e)
      {
      string currentcommand = e.CommandName;
      int currentrowindex = Int32.Parse(e.CommandArgument);
      string productid = GridView1.DataKeys[currentrowindex].Value;
      }

      R Offline
      R Offline
      Ryomin
      wrote on last edited by
      #2

      You could store the data in an arraylist and store that arraylist in the ViewState similar to:

      public ArrayList GetProductList
      {
      if(ViewState["ProductList"]==null)
      {
      ViewState["ProductList"] = new ArrayList();
      }
      return (ArrayList)ViewState["ProductList"];
      }

      public void AddProduct(int productId)
      {
      if(ViewState["ProductList"]==null)
      {
      ViewState["ProductList"] = new ArrayList();
      }

      ((ArrayList)ViewState["ProductList"]).Add(productId);
      }

      Then when you want to add something to your product list you can call it using

      AddProduct(productId);

      And you can cycle through your ArrayList:

      foreach(int productId in GetProductList().Items)
      {
      //execute code for productId
      }

      I'm not at my workstation at the moment, but this should help you get started on your problem. Best of luck!!! :)

      Cheers Disgyza Programmer Analyst

      S 1 Reply Last reply
      0
      • R Ryomin

        You could store the data in an arraylist and store that arraylist in the ViewState similar to:

        public ArrayList GetProductList
        {
        if(ViewState["ProductList"]==null)
        {
        ViewState["ProductList"] = new ArrayList();
        }
        return (ArrayList)ViewState["ProductList"];
        }

        public void AddProduct(int productId)
        {
        if(ViewState["ProductList"]==null)
        {
        ViewState["ProductList"] = new ArrayList();
        }

        ((ArrayList)ViewState["ProductList"]).Add(productId);
        }

        Then when you want to add something to your product list you can call it using

        AddProduct(productId);

        And you can cycle through your ArrayList:

        foreach(int productId in GetProductList().Items)
        {
        //execute code for productId
        }

        I'm not at my workstation at the moment, but this should help you get started on your problem. Best of luck!!! :)

        Cheers Disgyza Programmer Analyst

        S Offline
        S Offline
        strawberrysh
        wrote on last edited by
        #3

        I want to store Id in a list and list in a session.in another page I want to use these id in a gridview.my code is for shopping cart.

        M 1 Reply Last reply
        0
        • S strawberrysh

          I want to store Id in a list and list in a session.in another page I want to use these id in a gridview.my code is for shopping cart.

          M Offline
          M Offline
          MidwestLimey
          wrote on last edited by
          #4

          And? Just extract the list again as shown in the demo code .. If you want to store it in the session then change view state for session!? Not rocket science.


          I'm largely language agnostic


          After a while they all bug me :doh:


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups