c# - Trying to add rows to a Table results in multiple tables -


i'm trying add rows table when click button, i'm using session when page gets loaded again don't loose rows inserted. inserts 1 row , inserts them in table down.

protected void page_load(object sender, eventargs e) {          if (!ispostback)         {              gestorbd = new gestorbdt.gestorbd("msdaora", "fs", "pass", "oracle");             cadsql = "select * pcarticulos";             gestorbd.querydb(cadsql, "arts", dsgeneral);             c.loaddropdownlist(ddlarticulos, dsgeneral, "arts", "nombre");         }         if (session["ta"] != null)         {             tablaped = (table)session["ta"];             this.controls.add(tablaped);         }      }       protected void btadd_click(object sender, eventargs e)     {         table ta = tablaped;          tablerow t = new tablerow();         tablecell c = new tablecell();         c.text = ddlarticulos.selectedvalue.tostring();         t.cells.add(c);          tablecell c2 = new tablecell();         c2.text = tbcantidad.text;         t.cells.add(c2);          ta.rows.add(t);         session["ta"] = ta;     } 

gif of execution

aspx:

<!doctype html>  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="content-type" content="text/html; charset=utf-8"/>     <title></title> </head> <body style="background-color: #ffffff">     <form id="form1" runat="server">     <div style="height: 320px; background-color: #336699" id="div">          <h3>             <asp:label id="label1" runat="server" forecolor="white" style="font-weight: 700; font-size: large; font-family: 'segoe ui', tahoma, geneva, verdana, sans-serif" text="alta pedidos"></asp:label>         </h3>         <asp:label id="label2" runat="server" style="font-weight: 700; color: #ffffff; font-family: 'segoe ui', tahoma, geneva, verdana, sans-serif" text="artículos"></asp:label>         <asp:dropdownlist id="ddlarticulos" runat="server" style="z-index: 1; left: 122px; top: 55px; position: absolute; width: 201px; height: 19px;">         </asp:dropdownlist>         <br />         <asp:label id="label3" runat="server" forecolor="white" style="font-weight: 700; font-family: 'segoe ui', tahoma, geneva, verdana, sans-serif; z-index: 1; left: 12px; top: 90px; position: absolute" text="cantidad"></asp:label>         <asp:textbox id="tbcantidad" runat="server" style="z-index: 1; left: 120px; top: 92px; position: absolute; width: 194px; right: 839px"></asp:textbox>         <br />         <br />         <br />         <asp:button id="btagregat" runat="server" text="agregar la lista" onclick="btagregat_click" />         <br />         <br />         <asp:button id="button1" runat="server" onclick="button1_click" text="borrar lista" />         <br />         <br />          <asp:table id="tablaped" runat="server" style="color: #ffffff; font-family: 'segoe ui', tahoma, geneva, verdana, sans-serif; font-weight: 700; text-align: center;" width="378px">             <asp:tablerow runat="server">                 <asp:tablecell runat="server">articulos</asp:tablecell>                 <asp:tablecell runat="server">cantidad</asp:tablecell>             </asp:tablerow>         </asp:table>      </div>     </form> </body> </html> 

in page_load event, adding table altogether new control web page. should adding rows.

modify page_load event below,

    protected void page_load(object sender, eventargs e)             {                 if (!ispostback)                 {                     gestorbd = new gestorbdt.gestorbd("msdaora", "fs", "pass", "oracle");                    cadsql = "select * pcarticulos";                    gestorbd.querydb(cadsql, "arts", dsgeneral);                    c.loaddropdownlist(ddlarticulos, dsgeneral, "arts", "nombre");                 }                 if (session["ta"] != null)                 {                     tablaped.rows.addrange(((list<tablerow>)session["ta"]).toarray());                 }             } 

modify click event below,

protected void btadd_click(object sender, eventargs e) {         table ta = tablaped;         tablerow t = new tablerow();         tablecell c = new tablecell();         list<tablerow> listtablerows = session["ta"] != null ? (list<tablerow>)session["ta"] : new list<tablerow>();          c.text = ddlarticulos.selectedvalue.tostring():         t.cells.add(c);          tablecell c2 = new tablecell();         c2.text = tbcantidad.text:         t.cells.add(c2);          ta.rows.add(t);         listtablerows.add(t);         session["ta"] = listtablerows; } 

this work per requirement. let me know if face issue. can use viewstate instead of session, if want retain table in scope of page.

hope helps!


Comments