473,547 Members | 2,653 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 2047
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
914
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 table itself is not showing a new line, the row layout seems to be horizontal instead or vertical. How is possible? Is anyone there that can help me?...
5
2945
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 all the rows of ds1 to an existing table in an SQL Server database. I know I can do things like looping through the dataset and issuing Update SQL...
1
1359
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 QueryString (in the !isPostBack part of the Page_Load event) and use those information (in code behind, in !isPostBack part of Page_LOad event) to add some...
7
4372
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 was told that i could and even must not use it. GOD KNOWS WHY So i created a custom login form with userID and password which validates with a...
0
4247
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. That part works. However, my problem is that the form lets the user choose from input files; the labels are updated, the textboxes are not. This seems...
2
2949
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 selects the second row (rows) and the first cell (x). How would I select the first and second rows, and first - fifth rows, for example? I would think...
1
2229
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 text. The amount and sources of the images are determined by code analyzing the data, when the page is loaded. the second and third cells contain...
2
8763
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" style="width:140px;" /> </td></tr> </table>
0
1664
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 the GridView header where I type the number of rows I want to show in insert mode. Say I type 12. 2. When I click Add, the GridView changes to insert...
0
7510
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7437
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7463
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7797
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6032
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
748
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.