嘿所有我想要以正确的格式将我的数组放入ASP.net网页以获取订单表。
订单应该是:
model_number
comm_category
service
freight
sales_tax
sales
unit_price
price
id_price
目前输出如下:
model_number
price
unit_price
id_price
sales_tax
sales
service
freight
comm_category
因此我无法使用我的代码以正确的顺序附加到我的字符串:
Public Sub AssocArray_To_String(ByRef Output As System.Text.StringBuilder, ByVal AssocArrayInput As AssocArray)
For iParent As Integer = 0 To AssocArrayInput.Count - 1
Dim intX As Integer = 0
Dim arrParent As AssocArray = TryCast(AssocArrayInput.Item(iParent), AssocArray)
If arrParent Is Nothing Then Continue For
For iChild As Integer = 0 To arrParent.Count - 1
Call buildItems(PHPConvert.ToString(arrParent.Keys(iChild)), PHPConvert.ToString(arrParent.Values(iChild)), intX)
intX += 1
Next iChild
Next iParent
End Sub
Private Sub buildItems(ByVal nameOfItem As String, ByVal itemItself As String, ByVal intX As Integer)
Dim tmpStuff As Decimal = 0.0
Dim qty As Integer = 1
Dim model_number As String = ""
Dim comm_category As String = ""
Dim service As Double = 0
Dim freight As Double = 0
Dim sales_tax As Double = 0
Dim sales As Double = 0
Dim unit_price As Double = 0
Dim price As Double = 0
Dim id_price As Double = 0
If nameOfItem = "model_number" Then
model_number = itemItself
tableLoop += "<tr><td bgcolor=""#CCCCCC""><asp:Label ID=""item_count_" & intX & """ Text=""Label"">" & qty & "</asp:Label></td>"
tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""model_number_" & intX & """ Text=""Label"">" & model_number & "</asp:Label></td>"
ElseIf nameOfItem = "comm_category" Then
comm_category = itemItself
tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""comm_category_" & intX & """ Text=""Label"">" & comm_category & "</asp:Label></td>"
ElseIf nameOfItem = "service" Then
service = Format(Convert.ToDouble(itemItself), "$######.00")
tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""service_" & intX & """ Text=""Label"">" & service & "</asp:Label></td>"
ElseIf nameOfItem = "freight" Then
freight = Format(Convert.ToDouble(itemItself), "$######.00")
tableLoop += "<td><asp:Label ID=""freight_" & intX & """ Text=""Label"">" & freight & "</asp:Label></td>"
ElseIf nameOfItem = "sales_tax" Then
sales_tax = Format(Convert.ToDouble(itemItself), "$######.00")
tableLoop += "<td><asp:Label ID=""sales_tax_" & intX & """ Text=""Label"">" & sales_tax & "</asp:Label></td>"
ElseIf nameOfItem = "sales" Then
sales = Format(Convert.ToDouble(itemItself), "$######.00")
tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""sales_" & intX & """ Text=""Label"">" & sales & "</asp:Label></td>"
ElseIf nameOfItem = "unit_price" Then
unit_price = Format(Convert.ToDouble(itemItself), "$######.00")
tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""unit_price_" & intX & """ Text=""Label"">" & unit_price & "</asp:Label></td></tr>"
ElseIf nameOfItem = "price" Then
price = Format(Convert.ToDouble(itemItself), "$######.00")
tableLoop += "<td><asp:Label ID=""amount_" & intX & """ Text=""Label"">" & price & "</asp:Label></td>"
ElseIf nameOfItem = "id_price" Then
id_price = Format(Convert.ToDouble(itemItself), "$######.00")
tableLoop += "<td><asp:Label ID=""id_price_" & intX & """ Text=""Label"">" & id_price & "</asp:Label></td>"
End If
End Sub
最终结果如下:
<tr><td bgcolor="#CCCCCC"><asp:Label ID="item_count_0" Text="Label">1</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="model_number_0" Text="Label">TTN491-7BW</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="sales_1" Text="Label">3084.63</asp:Label></td>
<td><asp:Label ID="amount_2" Text="Label">3810</asp:Label></td>
<td><asp:Label ID="sales_tax_3" Text="Label">290.37</asp:Label></td>
<td><asp:Label ID="id_price_4" Text="Label">3810</asp:Label></td></tr>
<td bgcolor="#CCCCCC"><asp:Label ID="comm_category_5" Text="Label">X24</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="service_6" Text="Label">51</asp:Label></td>
<td><asp:Label ID="freight_7" Text="Label">384</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="unit_price_8" Text="Label">3135.63</asp:Label></td></tr>
哪个应输出如下:
<tr><td bgcolor="#CCCCCC"><asp:Label ID="item_count_0" Text="Label">1</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="model_number_0" Text="Label">TTN491-7BW</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="comm_category_5" Text="Label">X24</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="service_6" Text="Label">51</asp:Label></td>
<td><asp:Label ID="freight_7" Text="Label">384</asp:Label></td>
<td><asp:Label ID="sales_tax_3" Text="Label">290.37</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="sales_1" Text="Label">3084.63</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="unit_price_8" Text="Label">3135.63</asp:Label></td></tr>
<td><asp:Label ID="amount_2" Text="Label">3810</asp:Label></td>
<td><asp:Label ID="id_price_4" Text="Label">3810</asp:Label></td></tr>
如何以正确的顺序放置数组?
答案 0 :(得分:1)
为什么不直接引用您需要的项目,而不是循环关联键(以任意顺序返回):
Dim arrParent As AssocArray = TryCast(AssocArrayInput.Item(iParent), AssocArray)
If arrParent Is Nothing Then Continue For
Call buildItems("model_number", PHPConvert.ToString(arrParent.Values("model_number")), intX)
Call buildItems("comm_category", PHPConvert.ToString(arrParent.Values("model_number")), intX)
Call buildItems("service", PHPConvert.ToString(arrParent.Values("model_number")), intX)
Call buildItems("freight", PHPConvert.ToString(arrParent.Values("model_number")), intX)
'ETC
或者更好的是,完全摆脱BuildItems
并在上面的适当位置内联逻辑。
答案 1 :(得分:1)
您正在致电
For iChild As Integer = 0 To arrParent.Count - 1
Call buildItems(PHPConvert.ToString(arrParent.Keys(iChild)),
PHPConvert.ToString(arrParent.Values(iChild)),
intX)
intX += 1
Next iChild
因此,您将始终获得arrParent中项目的顺序。所以你要做的就是以正确的顺序手动调用buildItems(...)
Call buildItems(PHPConvert.ToString("model_number"),
PHPConvert.ToString(arrParent.Values("model_number")),
intX)
Call buildItems(PHPConvert.ToString("comm_category"),
PHPConvert.ToString(arrParent.Values("comm_category")),
intX)
' and so on
此外,我在buildItems中找不到tableLoop
的定义。看来这是一个简单的字符串变量。你应该使用 StringBuilder ,因为这个类在连接字符串方面更快!请参阅MSDN。