【问题标题】:adding row to table need to add to viewstate?向表中添加行需要添加到视图状态?
【发布时间】:2013-02-07 21:41:33
【问题描述】:

您好,我从一个单行 5 列的表格开始。我添加了一个按钮和返回代码来添加额外的行。第一行显示正常,但之后保持在 2 行。正在调试页面。我已经阅读了 viewstate 我只是不完全了解如何添加它,如果这有意义的话。

编辑

     <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

【问题讨论】:

  • 请向我们展示您的尝试。仅从文字很难理解。
  • 我并没有完全尝试添加到视图状态,这更像是我的问题。我不确定这实际上是如何完成的,但我添加了我目前拥有的代码。更新面板中的表格和单击按钮时添加另一行。当页面被发回时,前一行被删除,所以我从来没有超过 2 行。所以我想我需要将它添加到正确的视图状态?
  • 如果您使用像GridView 这样的网络数据绑定控件,您会更轻松。然后,您根本不需要摆弄 ViewState 或重新创建控件。这些控件应该是每个 ASP.NET Web 开发人员应该首先学习的。要回答您的问题:我在此处添加了带有动态控件和 ViewState 的完整示例:stackoverflow.com/questions/5266482/…
  • 谢谢,我想我最终会保留带有文本框的表格,然后单击按钮会将数据添加到网格视图中

标签: asp.net vb.net viewstate


【解决方案1】:

您不能在 ViewState 中存储 Table 对象,它不可序列化。考虑将您的代码更改为使用DataTable,否则您将需要手动编写代码来将您的表转换为 DataTable,这将是太多不必要的服务器处理。

将其更改为 DataTable 后,只需搜索 -“在 ViewState VB.NET 中存储 DataTable”,那里的示例已经足够多

【讨论】:

    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 2017-05-13
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多