Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2006, 01:45 AM
Registered User
 
Join Date: Jul 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to dynamically modify a table...

Hi

I am new to asp.net. I am trying to modify a table dynamically. I need to add rows to the table and remove rows dynamically.

//Sample code

public partial class TableView : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       //suppose I am using a Table _table to display data
       populateTable();
   }
   protected void populateTable()
   {
       //populate table from database
   }
   void removeRow(object sender, EventArgs e)
   {//
   }
   void addNewRow(object sender, EventArgs e)
   {//gets called on the click event of a button.
   }
}

Problem is that the change is not reflected properly. Can anyone please guide me what I need to do to achieve my objective. Please do reply. I am after this for the last three days, and I couldn't find any web resource as to how to do this.

Roopesh


roopesh
 
Old July 20th, 2006, 02:03 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

have u stored the table in any database? or is it just a table variable?

Regards
Ganesh
http://ganeshprof.blogspot.com
 
Old July 20th, 2006, 04:29 AM
Registered User
 
Join Date: Jul 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What I meant by table is the asp control, System.Web.UI.WebControl.Table

Roopesh

roopesh
 
Old July 20th, 2006, 04:31 AM
Registered User
 
Join Date: Jul 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And System.Web.UI.WebControl.Table is populated from the database.

Roopesh

roopesh
 
Old July 20th, 2006, 05:39 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

ok Roopesh,

You can iterate thru the rows collection and manipulate the rows

Table.Rows.Add
Table.Rows.Remove

Regards
Ganesh
http://ganeshprof.blogspot.com
 
Old July 20th, 2006, 09:55 AM
Registered User
 
Join Date: Jul 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did the same. Logic for adding and deleting rows is written in two event handlers (onClick()). After adding one it is not adding any more. When the page loads next time, it shows the previous table(table.Rows.Count increases when I add one row, but count goes back to previous count once I try to add another row). The reason for this as I understood is "Dynamic controls have to be recreated every time the page load". How can I recreate the dynamically created rows when the page loads next time? Is it by somehow calling the addition/deletion event handlers in the page load event?

Roopesh

roopesh
 
Old July 20th, 2006, 08:13 PM
Registered User
 
Join Date: Jul 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What I did is like the foll:
//Sample code
//In the .aspx page I have created <asp:Table id="table"....>


public partial class TableView : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       //suppose I am using a Table _table to display data
       populateTable();
   }
   protected void populateTable()
   {
       //populate table from database
       table.Rows.Add(new_row);
   }
   void removeRow(object sender, EventArgs e)
   {//
       table.Rows[index].Remove(a_row)
   }
   void addNewRow(object sender, EventArgs e)
   {//gets called on the click event of a button.
       table.Rows.Add(new_row);
   }
}


roopesh
 
Old July 21st, 2006, 03:19 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dim dt As DataTable
Dim dr As DataRow
Dim i As Integer

        'create a DataTable
        dt = New DataTable
        dt.Columns.Add(New DataColumn("ABC", GetType(DateTime)))
        dt.Columns.Add(New DataColumn("XXX", GetType(Double)))
        dt.Columns.Add(New DataColumn("YYY", GetType(String)))
        dt.Columns.Add(New DataColumn("AAA", GetType(Integer)))
        dt.Columns.Add(New DataColumn("ZZZ", GetType(Boolean)))


        'Make some rows and put some sample data in
        For i = 1 To 5
            dr = dt.NewRow()
            dr(0) = DateTime.Now.ToShortTimeString
            dr(1) = 1.23 * (i + 1)
            dr(2) = "Item " + i.ToString()
            dr(3) = 1.34 * (i + 1)
            dr(4) = 1.27 * (i + 1)
            dr(5) = True
            'add the row to the datatable
            dt.Rows.Add(CheckBoxField)
            dt.Rows.Add(dr)
        Next

        'return a DataView to the DataTable
        CreateDataSource = New DataView(dt)

    End Function
 
Old July 31st, 2006, 05:06 AM
Authorized User
 
Join Date: Oct 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i got ur problem. If you want to do this try to do the following,
Add one Table control in the form
When page loads create one DataTable and populate it with data from database. Assign this datatable to the datasource of TableControl.And save the datatable in session.
When you click add button,take datatable from session and add row to that and assign to Table Control and save in session again.
When you click remove button,take datatable from session and remove row from that and assign to Table Control and save in session again.

This way you can persist data in the table.

I think this solves ur problem. If u r creating controls dynamically then let me know,because you have to do more to persist data between postbacks.


--------------------------------------------------
Software professional from Bangalore,INDIA
ratheesh1982@gmail.com
 
Old January 16th, 2007, 12:05 PM
Registered User
 
Join Date: Jan 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I want to add and remove table rows at run time from a table server control.
This table further contains dynamically added controls mainly textboxes.
My problem is that while i 've been able to dynamically add rows to the table i 've been unsuccessfull at removing rows from the table.
I add rows by calling a createrow function from page_init and calling the same function in the addrow button click event.
In the dynamically added rows i 've added a delete button . on click of this button i try to remove rows. while the row gets removed from the table the state isn't persisted during postback. how should i remove rows from the table at runtime?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify field properties in linked table RickD Access 1 September 8th, 2006 06:58 AM
dynamically loading table using javascript bkalyanchakri Javascript How-To 1 November 1st, 2005 07:21 AM
Can't Modify or Delete table PatYoda SQL Server 2000 3 December 3rd, 2004 09:41 AM
How to automatically modify picture size in table Edward King HTML Code Clinic 2 November 19th, 2003 05:28 PM
Cannot modify data in table residing on SQL Serv sherr8 SQL Server 2000 14 June 6th, 2003 04:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.