473,503 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add rows with TextBox in cells to existing table. Maintain viewstate.

1 New Member
I have a table that consists of two rows with three cells each row. The cells contain textboxes. I am able to successfully add a new row with textboxes when a button is clicked. The problem is retaining the newly added row with the filled in textboxes on the next button click. I think I am handling my viewstate incorrectly.

This code adds the new row:
Expand|Select|Wrap|Line Numbers
  1. Private Sub addRowsInTable()
  2.         Dim TextID As Integer
  3.         Dim TextIDCount As String
  4.         TextID = Int32.Parse(ViewState("rows").ToString()) + 3
  5.         TextIDCount = TextID.ToString
  6.         Dim text As New TextBox()
  7.         'Text.AutoPostBack = False
  8.         Dim cell As New HtmlTableCell()
  9.         Dim row As New HtmlTableRow()
  10.  
  11.         text.ID = "TxtWhat" + TextIDCount
  12.         text.Attributes.Add("style", "width:371px")
  13.         cell.Controls.Add(text)
  14.         row.Cells.Add(cell)
  15.  
  16.         text = New TextBox()
  17.         cell = New HtmlTableCell()
  18.         text.ID = "TxtWho" + TextIDCount
  19.         text.Attributes.Add("style", "width:216px")
  20.         cell.Controls.Add(text)
  21.         row.Cells.Add(cell)
  22.  
  23.         text = New TextBox()
  24.         cell = New HtmlTableCell()
  25.         text.ID = "TxtWhen" + TextIDCount
  26.         cell.Controls.Add(text)
  27.         row.Cells.Add(cell)        
  28.         tblReasons.Rows.Add(row)
  29.         ViewState("Rows") = (Int32.Parse(ViewState("rows").ToString()) + 1).ToString
  30.  
  31.     End Sub
  32.  
  33. This is where I think I'm messing up.  ViewState("rows") shows as 0 when I do a step through.  It is not retaining the value from the above sub.
  34.  
  35. Protected Overrides Sub LoadViewState(ByVal savedState As Object)
  36.         MyBase.LoadViewState(savedState)
  37.         myRowcount = Int32.Parse(ViewState("rows").ToString())
  38.  
  39.         For i As Integer = 1 To myRowcount
  40.             Dim text As New TextBox()
  41.             Dim cell As New HtmlTableCell()
  42.             Dim row As New HtmlTableRow()
  43.  
  44.             text.ID = "TxtWhat" + (i + 3).ToString  
  45.             text.Attributes.Add("style", "width:371px")
  46.             cell.Controls.Add(text)
  47.             row.Cells.Add(cell)
  48.  
  49.             text = New TextBox
  50.             cell = New HtmlTableCell()
  51.             text.ID = "TxtWho" + (i + 3).ToString
  52.             text.Attributes.Add("style", "width:216px")
  53.             cell.Controls.Add(text)
  54.             row.Cells.Add(cell)
  55.  
  56.             text = New TextBox
  57.             cell = New HtmlTableCell()
  58.             text.ID = "TxtWhen" + (i + 3).ToString
  59.             cell.Controls.Add(text)
  60.             row.Cells.Add(cell)
  61.  
  62.             tblReasons.Rows.Add(row)
  63.         Next
  64. End Sub
Thanks
Aug 12 '08 #1
1 2038
Frinavale
9,735 Recognized Expert Moderator Expert
If you declare your textboxes outside of that function as Private members to your page...and initialize those textboxes in your Page_Init event you should be able to maintain the text in the textboxes.

Please check out the article on how to use dynamic controls in asp.net.
Aug 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
911
by: Fabrizio | last post by:
Hi, I need to dynamically add rows and cells with controls like textbox to an empty table. After adding controls to cells, i add cell to rows and at the end add the rows to the table, but the...
5
2941
by: Wayne Wengert | last post by:
I am using VB ASP.NET. In my page I convert an uploaded XML file to a dataset as follows: Dim ds1 As DataSet = New DataSet ds1.ReadXml(strPathName, XmlReadMode.Auto) Now I want to append...
1
1350
by: Neven Klofutar | last post by:
Hi, I have a problem, and it seems I have an error in my logic ... First I add a Table control on my webform (EnableViewState is True by default). Then I retrive some information vie...
7
4359
by: Nerrad | last post by:
Hi, i am currently working on a project which requires me to create a form to allow the user to change their password. I know there is a user-level security login which is easier and safer but...
0
4240
by: Zark3 | last post by:
Hi all, In a web form I use a for loop to dynamically add TextBox'es to a page (one for each item in an input file). I can get the input values and I can display them in Label's and TextBox'es....
2
2940
by: e271828 | last post by:
Is there a way to specify more than one row or cell collection in the JS table object? Consider this: var x=document.getElementById('myTable').rows.cells x.innerHTML="NEW CONTENT" This...
1
2224
by: renahkw | last post by:
How can I easily limit the amount of rows displayed in a table, and then navigate from page to page? Each row consists of 3 cells: the first, containing a varying number of images followed by some...
2
8757
by: Simon | last post by:
Hello, how can i get data from this table: <table> <tr><td>someText</td></tr> <tr><td> <input name="DataGrid1:_ctl2:textBox" type="text" value="someText" id="DataGrid1__ctl2_textBox"...
0
1659
by: Andy B | last post by:
I need to take an existing GridView that connects to a database and gets rows from a table and force it to show x rows in the insert mode. This is what should happen: 1. There is a TextBox in...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.