Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Updating ASP:Table

Status
Not open for further replies.

KidHoss

Programmer
May 22, 2000
27
US
I am using a button click event to add a row with two cells to a table contained on a panel on my page. Each cell contains a text box to allow the user to enter some data. The table starts out with two rows and if the user needs more, they are to click the button and add another row to the table. I can get it to work one time. The next time the page is rendered, the table is back to it's original starting state (two rows). I know this has to do with STATE manangement. That every time the page is returned to the server it is re-created in a state prior to my dynamically adding a row to the table...... but ..... I do not know what I need to do to correct this. How to I re-establish the state of the table every time a row is added to it?

Thanks for any suggestions.
 
You can add simply add items to viewstate by using:
Code:
ViewState("MyControl") = TextBox1
and retreive them using LoadControl like:
Code:
PlaceHolder1.Controls.Add(Me.LoadControl(ViewState("MyControl"))

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Thanks for the post. I tried adding the save of the updated table to the viewstate after I updated it and then tried to retrieve it in teh Page_Load event..... but .... I get an error saying my file must be an "ascx" file to allow this. Can I not do this in my code behind file? (I am working in VB.Net by the way).
 
Also ..... it is telling me that since it is a table I am working with ... it is not "Serializable" so I need to convert it.

I guess nothing is easy .... eh?
 
Sorry that was actually a misleading example as I meant to add a user control containing the textbox to the viewstate in the first example, so it should have been:
Code:
ViewState("MyControl") = "MyUserControl.ascx"

If you simply want to add an ASP.NET Web Control, then you can do so without using LoadControl.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
ca8msm,

Perhaps some code might help me ... because I still do not understand .... but I think you have the right answer.

I have a table on a panel:

Code:
<asp:panel id="pnlPurposePanel" runat="server" height="127px" EnableViewState="true">
<asp:Table id="tblPurposeTable" Runat="server">
<asp:TableRow EnableViewState="False">
<asp:TableCell EnableViewState="False" Width="359px">
<asp:TextBox runat="server" Width="359px" ID="txtPurpRow1Cell01"></asp:TextBox>
</asp:TableCell>
<asp:TableCell EnableViewState="False" Width="359px">
<asp:TextBox runat="server" Width="359px" ID="txtPurpRow1Cell02"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow EnableViewState="False">
<asp:TableCell EnableViewState="False" Width="359px">
<asp:TextBox runat="server" Width="359px" ID="txtPurpRow2Cell01"></asp:TextBox>
</asp:TableCell>
<asp:TableCell EnableViewState="False" Width="359px">
<asp:TextBox runat="server" Width="359px" ID="txtPurpRow2Cell02"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>

On the same panel there is a button:

Code:
<asp:Button id="btnAddPurpLines" Text="Add Lines" Runat="server"></asp:Button>

</asp:panel>

Then in my code behind file I allow the user to add more rows to the table:

Code:
Private Sub btnAddPurpLines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPurpLines.Click

Dim intNbrRows As Integer
intNbrRows = tblPurposeTable.Rows.Count
intNbrRows = intNbrRows + 1
Dim strBox01ID As String
strBox01ID = "txtPurpRow" & intNbrRows.ToString & "Cell01"
Dim textBox01 As New TextBox
textBox01.Width = Unit.Pixel(359)
textBox01.ID = strBox01ID
Dim strBox02ID As String
strBox02ID = "txtPurpRow" & intNbrRows.ToString & "Cell02"
Dim textBox02 As New TextBox
textBox02.Width = Unit.Pixel(359)
textBox02.ID = strBox02ID
Dim tempRow As New TableRow
Dim tempCell01 As New TableCell
Dim tempCell02 As New TableCell
tempCell01.Controls.Add(textBox01)
tempCell02.Controls.Add(textBox02)
tempRow.Cells.Add(tempCell01)
tempRow.Cells.Add(tempCell02)
tblPurposeTable.Rows.Add(tempRow)
intNbrRows = tblPurposeTable.Rows.Count
End Sub

This process works fine one time through. I get a third row added with the textboxes included. If I make the panel invisible or try to add another row to the same table, it does not work. It appears the table reverts back to it's two row original state and I am not understanding what you are trying to tell me to do .... though I believe it is the right answer.
 
Maybe this article which discusses how to use viewstate may get you started:


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top