2014-09-23 19 views
0

我從基於聯合類型的有條件工資報告的數據庫創建動態作業列表。爲了處理條件語句,我決定在cs頁面的頁面加載部分中構建數據集,以輸出到aspx頁面上的標籤。所有按預期工作。當我嘗試在HTML上運行JQuery或JavaScript來摺疊和擴展職位以顯示需求時,問題就出現了。使用JQuery,JS和Ajaxtookit有很多方法可以做到這一點。所有工作都像靜態HTML中的魅力。但是,.Net爲我的輸出添加了一個span標籤,這會在佈局中引發一些問題,從而防止腳本運行。然後我添加了一個JQ函數來刪除span標籤,然後運行其他腳本(我將這些腳本放在頁面的底部,以便在頁面加載後調用它們)。 Document.ready或window.load都沒有任何影響。看起來頁面加載和頁面呈現狀態可能是問題。如何讓JQ和JS在編寫html時看到它?由於JavaScript在頁面加載後看不到html

CS:

protected void Page_Load(object sender, EventArgs e) 
    { 

     String strConnString96 = WebConfigurationManager.ConnectionStrings["JobList_ConnectionString"].ConnectionString; 
     OleDbConnection con96 = new OleDbConnection(strConnString96); 
     OleDbCommand cmd96 = new OleDbCommand("SELECT * FROM JOBS WHERE (Status = 'Open') AND (Approved = 'Yes')", con96); 
     OleDbDataAdapter da96 = new OleDbDataAdapter(cmd96); 
     DataTable dtJobList = new DataTable(); 
     da96.Fill(dtJobList); 
     con96.Close(); 

     if (dtJobList == null || dtJobList.Rows.Count == 0) 
      lblJobList.Text = "<h3>No positions available at this time.</h3>"; 

     if (dtJobList != null && dtJobList.Rows.Count > 0) 
     { 
      System.Text.StringBuilder sb1 = new System.Text.StringBuilder(); 

      int i = 0; 
      foreach (DataRow row in dtJobList.Rows) 
      { 

       string JobTitle = row["Job_Title"].ToString(); 
       string Func = row["Functions"].ToString(); 
       string Qual = row["Qualifications"].ToString(); 
       string ReqNum = row["Req_Number"].ToString(); 
       string Low = row["Low_Range"].ToString(); 
       string High = row["High_Range"].ToString(); 
       string SalType = row["Salary_Type"].ToString(); 
       string Union = row["Union"].ToString(); 
       i++; 



       sb1.Append("<div id='JobNbr-" + i + "'><h4>" + JobTitle + "</h4><h5>Essential Functions</h5><p>" + Func + "</p><h5>Qualifications</h5><p>" + Qual + "<p>"); 

       if (Union == "Executive" || Union == "Local 6" || Union == "C93") 
       { 

        sb1.AppendLine("<h5>Starting Salary</h5><p>" + Low + " - " + High + " <i>(" + SalType + ")</i>");                   
       } 

       else 
       { 
        sb1.AppendLine("<h5>Starting Salary</h5><p>" + High + " <i>(" + SalType + ")</i>"); 

       } 

       sb1.AppendLine("<a href='/HR_WebForm/HR_Form.aspx?Req_Number=" + ReqNum + "&Job_Title=" + JobTitle + "'><img src='/Resources/icons/apply.gif' alt='Apply' /></a></p></div>"); 

       lblJobList.Text = sb1.ToString(); 
      } 


     } 
    } 

ASPX:

<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolderMain" runat="server"> 



<h1>Employment</h1> 

    <div id="Employment"> 
    <h2>Join New England's largest retail water &amp; sewer provider!</h2> 

<h2>How to Apply:</h2> 

<ol><li>Submit your resume and cover letter online, please send attachments in PDF or Microsoft Word format, if not please fax to 617-989-7754 OR</li> 

<li>Visit our Human Resources Department at 980 Harrison Avenue Boston, MA 02119</li></ol> 

<p>We are located on several MBTA bus routes and visitors' parking is available on-site. Our office is open Monday through Friday, 8:00AM to 5:00PM. 
</p> 

<p>Come join our team!</p> 

<h2>Current Openings</h2> 

<div id="CurrentOpenings"> 

    <a id="On" name="On" onclick="openAll('CurrentOpenings', 'h4','');" href="#_" class="hideIfNoJS">Open All</a> 
    <a id="Off" name="Off" onclick="closeAll('CurrentOpenings', 'h4','');" href="#_" class="hideIfNoJS">Close All</a> 

<asp:Label ID="lblJobList" runat="server" Text="Label" /> 


</div> 

    <script>window.onload = "setCollapseExpand('CurrentOpenings', 'h4',''); revealControl('On'); revealControl('Off');"</script> 
<script type="text/javascript" src="/JScode/expandcollapse.js"></script> 
</asp:Content> 

回答

0

要設置在window.onload爲一個字符串。

window.onload = "setCollapseExpand('CurrentOpenings', 'h4',''); revealControl('On'); revealControl('Off');"</script> 

它期待着一個功能。既然你標記了這個jQuery,使用它。

$(window).on("load", function() { 
    setCollapseExpand('CurrentOpenings', 'h4',''); 
    revealControl('On'); 
    revealControl('Off'); 
}); 
+0

謝謝,但沒有奏效。我正在使用母版頁。原本onload是在身體標籤。同樣的方法在靜態的FAQ頁面上工作。我試過其他的展開/摺疊腳本,沒有一個能夠處理動態輸出。我甚至無法讓JQ看到span標籤。如果它不是用於條件輸出,我會使用listview。嘗試用內聯代碼處理它,但無法獲得嵌入代碼塊中的eval表達式。我已將clientID設置爲static,但不會阻止span標記將其自身附加到我的代碼中。啊! – 2014-09-23 14:42:01

+0

我找到了一個解決方案。根據.Net文檔,動態生成的內容不一定嚴格遵守頁面生命週期,從而允許時間上的某些延遲。當我在body標籤下添加一個新的內容佔位符時,這會增加足夠的延遲,以便在調用JS之前對錶進行渲染。簡單地把JS代碼放在關閉主體標籤之前的最後一個元素並沒有削減它。看起來,每個佔位符在進入下一個佔位符之前都完全呈現。在句子結尾處表現得像一段時間,只需要一點點停頓。 – 2014-09-26 12:05:55

+0

除非內容由​​Ajax加載,否則代碼應該在那裏加載。 – epascarello 2014-09-26 12:28:39