【发布时间】:2016-08-19 23:42:47
【问题描述】:
我正在使用 field.name 和 field.value 从 ADO 记录集中直接创建一个表。从之前的另一篇帖子Retrieve ADO Recordset Field names (Classic ASP) 可以看出。
更进一步,我试图根据来自另一个数据库的值重命名表头。基本上如果th = Column_1 然后写“名字”等。
我可以使用带有特定代码的“if then”函数单独执行此操作。但是,我有另一个数据库,它具有所有正确的标题名称,并且宁愿通过记录集使用 while 循环。而不是写每一行 - columnheading1=x 和 columnheading2=y 等,我宁愿创建一个 while 循环。
再一次,单个“if then”语句可以正常工作,如下面的代码所示,但记录集不能。
我试图修复记录集和循环,但没有好处。任何想法为什么 while 循环(或重复区域)在函数中不起作用?
下面是例子:
<table>
<thead>
<tr>
<%
Function Header_Name_Sub(Header_NameX)
Header_Name_Write = Header_NameX
If Header_Name_Write = "Project_File_ID" Then
Header_Name_Write = "File ID" ' this works great.
End If
If Header_Name_Write = "Project_Assignment_Name" Then
Header_Name_Write = "Project Name" ' this works great.
End If
' Have a data base of all header names based on column name ... this repeat is not working in the function. If I pull it out this section works fine elsewhere in the page...
Do While Not RS_Build_Numeric_Names.EOF
If CStr(Header_Name_Write) = CStr((RS_Header.Fields.Item("True_Column_Name").Value)) Then ' thought maybe string issue is why CStr
Header_Name_Write = (RS_Build_Numeric_Names.Fields.Item("Fixed_Column_Name").Value
End If
Loop
If Header_Name_Write = "Project_City" Then
Header_Name_Write = "City" ' this works great.
End If
End Function
%>
<%For Each fld in RS.Fields%>
<%
Header_Name_Sub(fld.Name)
%>
<th><span><%=Header_Name_Write%></span></th>
<%Next %>
</tr>
</thead>
<tbody>
<%
Do Until RS.EOF
OutputRow RS.Fields
RS.MoveNext
Loop
%>
</tbody>
</table>
<%
Sub OutputRow(fields)
%>
<tr>
<%For Each fld in fields%>
<td><span><%=(fld.Value)%></span></td>
<%Next %>
</tr>
<%
End Sub
%>
所以这是我刚刚意识到的简单形式的编辑。在函数内...
If Header_Name_Write = "Project_File_ID" Then
Header_Name_Write = "File ID" ' this works great.
End If
上面效果很好。我试图使用while循环来编写50多个额外的'If Then's',而不是它只是循环。那么如何在函数中写extra的呢?
If x = 1 Then Header_Name_Write = "File ID" End If
If x = 2 Then Header_Name_Write = "Next Header" End If
If x = 3 Then Header_Name_Write = "Another Header" End If
它是一个 for 循环吗?
再往前走一点……
For Each fld in RS_Build_Numeric_Names.Fields
If Header_Confirm = (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Column_Name").Value) Then
Header_Name_Write = (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Name").Value)
End If
Next
如果我使用 for 语句,则仅对记录集中的第一条记录有效...而不是整个循环。
【问题讨论】:
-
我应该使用 Server.CreateObject("Scripting.Dictionary") 吗?
标签: function vbscript asp-classic recordset