错误列不属于表 [英] error column does not belong to table
问题描述
您好我有几个文本框的表,当用户点击该按钮将其添加到一个GridView。它的工作罚款共享dtValues作为新的DataTable(),但我遇到了其他问题,所以我把它改成公共....在按钮preSS是我得到的错误列'数量'不属于表。有什么建议?
ASPX
< ASP:表ID =tblPrice=服务器网格=两者>
< ASP:&的TableRow GT;
< ASP:TableCell的>及数量LT; / ASP:TableCell的>
< ASP:TableCell的>材料与LT; / ASP:TableCell的>
< ASP:TableCell的>成本与LT; / ASP:TableCell的>
< ASP:TableCell的>价格< / ASP:TableCell的>
< ASP:TableCell的>总计< / ASP:TableCell的>
< / ASP:&的TableRow GT;
< ASP:&的TableRow GT;
< ASP:TableCell的>
< ASP:文本框ID =txtQty=服务器WIDTH =40文本=1>< / ASP:文本框>
< / ASP:TableCell的>
< ASP:TableCell的>
< ASP:文本框ID =txtMaterials=服务器>< / ASP:文本框>
< / ASP:TableCell的>
< ASP:TableCell的>
< ASP:文本框ID =txtCost=服务器WIDTH =125文本=0>< / ASP:文本框>
< ASP:MaskedEditExtender ID =meeCost1=服务器面具=99,999,999.99DisplayMoney =左的TargetControlID =txtCostMaskType =号码>
< / ASP:MaskedEditExtender>
< / ASP:TableCell的>
< ASP:TableCell的>
< ASP:文本框ID =txtPrice的AutoPostBack =真=服务器WIDTH =125文本=0OnTextChanged =PriceChange>< / ASP:文本框>
< ASP:MaskedEditExtender ID =meePrice1=服务器面具=99,999,999.99DisplayMoney =左的TargetControlID =txtPriceMaskType =号码>
< / ASP:MaskedEditExtender>
< / ASP:TableCell的>
< ASP:TableCell的>
< ASP:标签ID =lblTotal=服务器文本=$ 0.00>< / ASP:标签>
< / ASP:TableCell的>
< / ASP:&的TableRow GT;
< ASP:&的TableRow GT;
< ASP:TableCell的>
< ASP:按钮的ID =btnAddPurchase=服务器文本=添加购买的OnClick =btnAddPurchase_Click/>
< / ASP:TableCell的>
< / ASP:&的TableRow GT;
< / ASP:表>
< BR />
< ASP:GridView控件ID =gvPurchases=服务器的AutoGenerateColumns =FALSE
AutoGenerateDeleteButton =真AutoGenerateEditButton属性=真
AutoGenerateSelectButton =真>
< / ASP:GridView的>
code背后
公共类_Default
继承System.Web.UI.Page
公共dtValues作为新的DataTable()保护小组的Page_Load(发送者为对象,E作为EventArgs的)
如果没有的IsPostBack然后
CreateDataTable()
万一
结束小组
保护小组CreateDataTable()
dtValues.Columns.Add(新的DataColumn(数量))
dtValues.Columns.Add(新的DataColumn(材料))
dtValues.Columns.Add(新的DataColumn(费用))
dtValues.Columns.Add(新的DataColumn(价格))
dtValues.Columns.Add(新的DataColumn(合计))
结束小组
保护小组btnAddPurchase_Click(发送者为对象,E作为EventArgs的)
昏暗drValues作为的DataRow = dtValues.NewRow() drValues(数量)= txtQty.Text
drValues(材料)= txtMaterials.Text
drValues(成本)= txtCost.Text
drValues(价格)= 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
结束小组
有发生,因为仅在初始页面加载,当它通过点击按钮贴后背,表被重新初始化,但因为的IsPostBack没有列创建的表在Page_Load事件属实。因此,通过删除的IsPostBack条件,表对象实例为每个页面请求列。
保护小组的Page_Load(发送者为对象,E作为EventArgs的)
CreateDataTable()
结束小组
Hello I have a table with a few textboxes and when a user clicks the button it gets added to a gridview. It worked fine with Shared dtValues As New DataTable() but I ran into other issues so I changed it to Public .... On the button press is where I get the error Column 'Qty' does not belong to the table. Any suggestions?
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>
code Behind
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
It is happening because the table is created only on initial page load and when it is posted back via button click, table is again initialized but with no columns since IsPostBack is true in Page_Load event. So, by removing the IsPostBack condition, table object is instantiated with columns for every page request.
Protected Sub Page_Load(sender As Object, e As EventArgs)
CreateDataTable()
End Sub
这篇关于错误列不属于表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!