Pls I have a table generated at runtime.
each column have 2 IMAGE button (ADD AND DELETE)
IF THE USER CLICKS ADD BUTTON, THEN A ROW IS DYNAMICALLY ADDED AND IF THE DELETE IS CLICKED THEN THE CURRENT ROW IS DELETED.

hOW DO I GO ABOUT THIS. AM USING VB.NET (ASP.NET)
THANKS

Dani AI

Generated

Two practical patterns work well for runtime add/delete rows in ASP.NET WebForms: a server-side DataTable bound to a GridView (easier for state and server events), or a client-side approach (JavaScript/jQuery) that manipulates DOM rows and posts the final data. As asked about a DataTable, the snippet below demonstrates a robust server-side pattern that keeps row data in ViewState, supports an Add and Delete ImageButton per row, and preserves user edits before changing rows. This also avoids the common pitfall where dynamic controls lose event wiring (GridView declared in markup avoids manual recreate-on-Init).

Example markup (GridView with two ImageButtons in each row):

<asp:GridView ID="gvRows" runat="server" AutoGenerateColumns="False" OnRowCommand="gvRows_RowCommand">
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:TextBox ID="txtVal" runat="server" Text='<%# Eval("Value") %>' />
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:ImageButton ID="imgAdd" runat="server" ImageUrl="~/add.png"
          CommandName="AddRow" CommandArgument='<%# Container.DataItemIndex %>' />
        <asp:ImageButton ID="imgDel" runat="server" ImageUrl="~/del.png"
          CommandName="DeleteRow" CommandArgument='<%# Container.DataItemIndex %>' />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

<asp:Button ID="btnAddNew" runat="server" Text="Add New Row" OnClick="btnAddNew_Click" />

Key points in code-behind (VB.NET):

  • Keep the data in a DataTable stored in ViewState (or Session for large data).
  • Before inserting/removing rows, pull current textbox values from GridView rows into the DataTable so user edits are not lost.
  • Use the GridView RowCommand to handle AddRow/DeleteRow; CommandArgument gives the row index.
  • Rebind after every change.

Cautions and tips:

  • For dynamic server controls created purely in code (not declared in markup), recreate them in Page_Init on every request with the same IDs so events fire.
  • Avoid large ViewState storage; for many rows prefer Session or a temporary DB table.
  • If events are not firing, it usually means controls were not recreated early enough or IDs changed.

Recommended Answers

All 2 Replies

Show us your code work please. Are you working with DataTable instance?

No, not at all
All i did is just create a table of two row at runtime with two button and a textbox.

Button 1 for add and button 2 for delete
thanks
pls. all help is appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.