2
votes

Hello I start with a table with a single row and 5 columns. I have added a button and back code to add additional rows. The first row shows up fine but it stays at 2 rows after that. Debugging the page is being fired. I have read up on viewstate I'm just not exactly understanding how to add to it, if that makes sense.

EDIT

     <asp:UpdatePanel ID="upTable" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
    <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnNewRow" EventName ="click" />
    </Triggers> 
    <ContentTemplate>
            <div>
                <asp:Table ID="tblPrice" runat="server" GridLines="Both">
                    <asp:TableRow>
                        <asp:TableCell>QTY.</asp:TableCell>
                        <asp:TableCell>MATERIAL</asp:TableCell>
                        <asp:TableCell>COST</asp:TableCell>
                        <asp:TableCell>PRICE</asp:TableCell>
                        <asp:TableCell>TOTAL</asp:TableCell>
                    </asp:TableRow>
                    <asp:TableRow>
                        <asp:TableCell>
                            <asp:TextBox ID="tbQty1" runat="server" Width="40" Text="0"></asp:TextBox>
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:TextBox ID="tbMaterial1" runat="server"></asp:TextBox>
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:TextBox ID="tbCost1" runat="server" OnTextChanged="PriceChange" AutoPostBack ="true" Text="0"></asp:TextBox>
                            <asp:MaskedEditExtender ID="meeCost1" runat="server" Mask="99,999,999.99" DisplayMoney="Left" TargetControlID="tbCost1" MaskType="Number">
                            </asp:MaskedEditExtender>
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:TextBox ID="tbPrice1" runat="server" OnTextChanged="PriceChange" AutoPostBack="true"  Text="0"></asp:TextBox>
                            <asp:MaskedEditExtender ID="meePrice1" runat="server" Mask="99,999,999.99" DisplayMoney="Left" TargetControlID="tbPrice1" MaskType="Number">
                            </asp:MaskedEditExtender>
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Label ID="lblTotal1" runat="server" Text="$0.00"></asp:Label>
                        </asp:TableCell>
                    </asp:TableRow>
                </asp:Table>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="btnNewRow" runat="server" Text="Add Row" />

vb

Protected Sub btnNewRow_Click(sender As Object, e As EventArgs) Handles btnNewRow.Click
    Dim TRow As New TableRow()
    Dim qtyCell As New TableCell()
    Dim materialCell As New TableCell()
    Dim costCell As New TableCell()
    Dim priceCell As New TableCell()
    Dim totalCell As New TableCell()

    Dim qtyTB As New TextBox()
    Dim materialTB As New TextBox()
    Dim costTB As New TextBox()
    Dim priceTB As New TextBox()

    Dim costMEE As New AjaxControlToolkit.MaskedEditExtender
    Dim priceMEE As New AjaxControlToolkit.MaskedEditExtender

    Dim rowsCount As Integer = tblPrice.Rows.Count
    Dim rowsString As String = rowsCount.ToString

    qtyTB.Width = 40

    'configure the masked edit extender controls
    With costMEE
        .ID = "meeCost" & rowsString
        .Mask = "99,999,999.99"
        .DisplayMoney = AjaxControlToolkit.MaskedEditShowSymbol.Left
        .TargetControlID = "tbCost" & rowsString
        .MaskType = AjaxControlToolkit.MaskedEditType.Number
    End With
    With priceMEE
        .ID = "meePrice" & rowsString
        .Mask = "99,999,999.99"
        .DisplayMoney = AjaxControlToolkit.MaskedEditShowSymbol.Left
        .TargetControlID = "tbPrice" & rowsString
        .MaskType = AjaxControlToolkit.MaskedEditType.Number
    End With

    'Add masked edit extender to textboxes
    costTB.Controls.Add(costMEE)
    priceTB.Controls.Add(priceMEE)


    'add id names to the textboxes
    qtyTB.ID = "tbQty" & rowsString
    materialTB.ID = "tbMaterial" & rowsString
    costTB.ID = "tbCost" & rowsString
    priceTB.ID = "tbPrice" & rowsString

    'Add textbox to the table cells
    qtyCell.Controls.Add(qtyTB)
    materialCell.Controls.Add(materialTB)
    costCell.Controls.Add(costTB)
    priceCell.Controls.Add(priceTB)

    'Add table cells to the table row
    TRow.Cells.Add(qtyCell)
    TRow.Cells.Add(materialCell)
    TRow.Cells.Add(costCell)
    TRow.Cells.Add(priceCell)
    TRow.Cells.Add(totalCell)

    'Add table row to the table
    tblPrice.Rows.Add(TRow)
End Sub
1
Please show us what you have tried. It's difficult to understand from words only.Tim Schmelter
I didn't exactly try adding to the viewstate that's more my question. I'm not sure how this is actually done but I added my code for what I currently have. A table in an update panel and when a button is clicked another row is added. When the page gets posted back the previous row gets deleted so I never have more then 2 rows. So I figured I would need to add it to the viewstate correct?joetinger
You would make it much more easy if you'd use web-databound controls like GridView. Then you don't need to fiddle around with ViewState or recreating controls at all. These controls should be the first every ASP.NET web developer should learn. To answer your question: i've added a full sample with dynamic controls and ViewState here: stackoverflow.com/questions/5266482/…Tim Schmelter
Thanks I think I'll end up keeping the table with the textboxes and then on button click it will add the data to a gridviewjoetinger
Or maybe even something like dotnetfunda.com/articles/article180.aspxjoetinger

1 Answers

1
votes

You can't store a Table object in ViewState, it's not serializable. Consider changing your code to use a DataTable, otherwise you will need to manually write code to convert your table to a DataTable and that will just be too much unnecessary server processing.

Once you've changed it to a DataTable just search for - "Storing DataTable in ViewState VB.NET" there are more than enough examples out there