2016-08-19 48 views
0

我使用field.name和field.value直接从ADO记录集创建表。从另一个之前的帖子Retrieve ADO Recordset Field names (Classic ASP)可以看出。
更进一步,我试图根据来自另一个数据库的值重命名表头。本质上,如果th = Column_1然后写“名”等使用while循环ASP vbscript创建函数?

我可以用一个“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'而不是循环播放。那么我该如何在函数中写入额外的?

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 

如果我使用了statment只在记录......而不是整个循环做的第一条记录工作。

+0

我应该使用的Server.CreateObject( “的Scripting.Dictionary”)? – Brewy

回答

0

因为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 
. 
. 
. 
+0

感谢回复,我同意帮助清理代码。仍然无法获得返回标题的时间。仍在尝试! – Brewy

+0

刚想到我为什么循环不起作用。因为它不断重新定义循环内的值。 Doooop!我希望循环将函数写入所有'if then'语句中...不能替换该值。我怎么能做到这一点? – Brewy

0

想通了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> 

现在我只是不能让其他人填写。意思是如果它不符合名称我想要默认名称。足够近!

0

刚想到我为什么循环不起作用。因为它不断重新定义循环内的值。 Doooop!我希望循环将函数写入所有'if then'语句中...不能替换该值。我怎么能做到这一点? -

变量=变量& “新数据”