【问题标题】:Create a Function with while loop ASP vbscript?用while循环ASP vbscript创建一个函数?
【发布时间】: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


【解决方案1】:

因为Header_Name_Sub() 是一个函数,你应该让它正确地返回结果,而不是使用Header_Name_Write 变量。在您的代码中,Header_Name_Write 应该是一个全局变量,但由于您没有使用 DIM 语句显式声明它,因此可能会产生意外结果。

您应该尝试以下方法:

<%
  Function Header_Name_Sub(Header_NameX)
    Dim Header_Name_Write '' make it local here
    Header_Name_Write = Header_NameX
    .
    .
    .
    .
    Header_Name_Sub = Header_Name_Write '' assign the local value and return it
  End Function

  For Each fld in RS.Fields
    %><th><span><%=Header_Name_Sub(fld.Name)%></span></th><%
  Next
.
.
.

【讨论】:

  • 感谢您的回复,我同意也有助于清理代码。仍然无法获得返回标题的时间。还在努力!
  • 我刚刚明白为什么循环不起作用。因为它不断地重新定义循环内的值。嘟嘟!我希望循环将所有需要的“if then”语句写入函数......而不是替换值。我怎样才能做到这一点?
【解决方案2】:

想通了 99%。使用脚本字典来定义标题名称是正确的。

<table border="1px">
  <thead>
      <tr>

<%
Set Header_Options_All = Server.CreateObject("Scripting.Dictionary")
Do While Not RS_Header_Options_2.eof 'one RS Loop
    Header_Options_All.Add (RS_Header_Options_2.Fields.Item("Build_Project_Drop_Column_Name").Value), (RS_Header_Options_2.Fields.Item("Build_Project_Drop_Option_Name").Value)
    RS_Header_Options_2.movenext
Loop
Do While Not RS_Build_Numeric_Names.eof 'Another RS Loop
    Header_Options_All.Add (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Column_Name").Value), (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Name").Value)
    RS_Build_Numeric_Names.movenext
Loop
Header_Options_All.Add "Project_File_ID", "File ID" 'singles I added
Header_Options_All.Add "Project_ID", "ID"
Header_Options_All.Add "Project_Assignment_Name", "Project Name"
Header_Options_All.Add "Project_City", "City"
%>

<%
Function Header_Name_Sub(Header_NameX)
Dim Header_Name_Write
Dim Header_Confirm
Header_Name_Write = Header_NameX
Header_Confirm = Header_NameX 
For Each elem In Header_Options_All
Header_Name_Write = Header_Options_All.Item(Header_Confirm)
Next
Header_Name_Sub = Header_Name_Write
End Function
%>      

         <%For Each fld in RS_Full_List_Building_Inner_Join.Fields%>
<th><span><%=Header_Name_Sub(fld.Name)%></span></th>
         <%Next %>
      </tr>
  </thead>

现在我无法让其他人填写。这意味着如果它与名称不匹配,我想要默认名称。够近了!

【讨论】:

    【解决方案3】:

    我刚刚明白为什么循环不起作用。因为它不断地重新定义循环内的值。嘟嘟!我希望循环将所有需要的“if then”语句写入函数......而不是替换值。我怎样才能做到这一点? -

    变量 = 变量 & “新数据”

    【讨论】:

      猜你喜欢
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      相关资源
      最近更新 更多