2016-08-19 65 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'語句中...不能替換該值。我怎麼能做到這一點? -

變量=變量& 「新數據」