1

您好,我有一个带有几个文本框的表格,当用户单击按钮时,它会被添加到网格视图中。它与 Shared dtValues As New DataTable() 一起工作得很好,但我遇到了其他问题,所以我将其更改为 Public ...。在按钮按下时,我得到错误 Column 'Qty' does not belongs to the table。有什么建议么?

aspx

                <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="txtQty" runat="server" Width="40" Text = "1"></asp:TextBox>
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:TextBox ID="txtMaterials" runat="server"></asp:TextBox>
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:TextBox ID="txtCost" runat="server" Width="125" Text = "0" ></asp:TextBox>
                        <asp:MaskedEditExtender ID="meeCost1" runat="server" Mask="99,999,999.99" DisplayMoney="Left" TargetControlID="txtCost" MaskType="Number">
                        </asp:MaskedEditExtender>
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:TextBox ID="txtPrice" AutoPostBack="true" runat="server" Width="125" Text = "0" OnTextChanged = "PriceChange"></asp:TextBox>
                        <asp:MaskedEditExtender ID="meePrice1" runat="server" Mask="99,999,999.99" DisplayMoney="Left" TargetControlID="txtPrice" MaskType="Number">
                        </asp:MaskedEditExtender>
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:Label ID="lblTotal" runat="server" Text="$0.00"></asp:Label>
                    </asp:TableCell>                
                </asp:TableRow>
                <asp:TableRow>
                    <asp:TableCell>
                        <asp:Button ID="btnAddPurchase" runat="server" Text="Add Purchase" OnClick="btnAddPurchase_Click"  />
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
            <br />
            <asp:GridView ID="gvPurchases" runat="server" AutoGenerateColumns="False" 
                AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
                AutoGenerateSelectButton="True" >
            </asp:GridView>

后面的代码

Public Class _Default
Inherits System.Web.UI.Page
Public dtValues As New DataTable()

Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        CreateDataTable()
    End If
End Sub
 Protected Sub CreateDataTable()
    dtValues.Columns.Add(New DataColumn("Qty"))
    dtValues.Columns.Add(New DataColumn("Materials"))
    dtValues.Columns.Add(New DataColumn("Cost"))
    dtValues.Columns.Add(New DataColumn("Price"))
    dtValues.Columns.Add(New DataColumn("Total"))
End Sub

Protected Sub btnAddPurchase_Click(sender As Object, e As EventArgs)

    Dim drValues As DataRow = dtValues.NewRow()

    drValues("Qty") = txtQty.Text
    drValues("Materials") = txtMaterials.Text
    drValues("Cost") = txtCost.Text
    drValues("Price") = txtPrice.Text

    dtValues.Rows.Add(drValues)

    gvPurchases.DataSource = dtValues
    gvPurchases.DataBind()

    txtQty.Text = "1"
    txtMaterials.Text = ""
    txtCost.Text = "0"
    txtPrice.Text = "0"
    lblTotal.Text = "$0.00"
End Sub
4

1 回答 1

0

发生这种情况是因为该表仅在初始页面加载时创建,并且当通过按钮单击回发时,表再次初始化但没有列,因为 IsPostBack 在 Page_Load 事件中为真。因此,通过删除 IsPostBack 条件,表对象被实例化为每个页面请求的列。

Protected Sub Page_Load(sender As Object, e As EventArgs)                 
    CreateDataTable()
End Sub
于 2013-02-11T18:30:02.443 回答