2013-05-19 27 views
0

大家好,我正在構建一個簡單的aspx網頁,它在字典中查找單詞(使用查詢字符串獲取url),將結果注入佔位符並將其格式內容適當地從字典網站檢索一個css文件。注入的CSS文件不適用於注入標記ASP.NET 4

顯然我不知道如何解決第三點,事實上我已經嘗試了多種方法,但似乎沒有工作。

準確地說,我已成功檢索到正確的css文件的鏈接並將其注入頁頭(嘗試:Page_load,Page_Init事件),查詢結果被注入到Button_click事件的'resultpanel'佔位符中處理程序。

我還收錄了我的ASPX標記,接着是相應的代碼隱藏文件 此項目使用HtmlAgilityPack。

背後

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net; 
using System.IO; 
using HtmlAgilityPack; 
using System.Web.UI.HtmlControls; 

namespace Multidictionary 
{ 
    public partial class Default : System.Web.UI.Page 
    { 
     private string dictionaryUrl = "http://www.vandale.nl"; 
     private IEnumerable<HtmlNode> csslink; 

     protected void Page_Init(object sender, EventArgs e) 
     { 

     } 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      //your code 
      string url = TextBoxURL.Text; 
      var webGet = new HtmlWeb(); 
      var document = webGet.Load(url); 
      csslink = (from el in document.DocumentNode.SelectNodes("//link[@type='text/css']") 
         where 
         (el.Attributes["rel"] != null) && ("stylesheet".Equals(el.Attributes["rel"].Value)) 
         select el); 
      if (csslink != null && csslink.Count() > 0) 
      { 
       HtmlLink cssLink = new HtmlLink(); 
       cssLink.Href = dictionaryUrl + csslink.FirstOrDefault().Attributes["href"].Value;//"path to CSS"; 
       cssLink.Attributes["type"] = "text/css"; 
       cssLink.Attributes["media"] = "all"; 
       hdr.Controls.Add(cssLink); 
      } 
     } 
     protected void Button1_Click(object sender, EventArgs e) 
     { 
      string url = TextBoxURL.Text; 
      var webGet = new HtmlWeb(); 
      var document = webGet.Load(url); 
      var definitieDiv = from el in document.DocumentNode.Descendants() 
           where (el.Attributes["id"] != null) && ("content-area".Equals(el.Attributes["id"].Value)) 
           select el; 
      if (definitieDiv != null && definitieDiv.Count() > 0) 
      { 
       resultpanel.Text = definitieDiv.FirstOrDefault().OuterHtml; 
      } 

     } 
    } 
} 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Multidictionary.Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <asp:PlaceHolder runat="server" id="hdr"></asp:PlaceHolder> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <h3> 
      Enter URL and get contents of the page</h3> 
     <asp:textbox id="TextBoxURL" runat="server" height="20px" width="250px" text="http://www.vandale.nl/opzoeken?pattern=kat&lang=nn"> 
     </asp:textbox> 
     <asp:button id="Button1" runat="server" text="Get Contents" onclick="Button1_Click" /> 
     <br /> 
     <asp:literal runat="server" id="resultpanel"></asp:literal> 
    </div> 
    </form> 
</body> 
</html> 

法典是我的問題需要任何澄清,請不要猶豫與我聯繫。

回答

0

您是否在瀏覽器中檢查了生成的html?我期望樣式表在URL的域部分缺少。

+0

謝謝您的關注,是的,我有,顯然一切都是正確的: <鏈接HREF =「http://www.vandale.nl/sites/default/files/css/css_f50d4c2c6666e759d4a7cd8dbf8d2a88.css 「type =」text/css「media =」all「> 我懷疑它與兩次注射的順序有關。我也檢查了我正在檢索正確的CSS文件。 –

+0

我錯過了vandals.nl前面的http關鍵字 –

+0

我檢查過「Show Source」,它在那裏。如果它沒有出現在我的評論中,那是因爲它通過堆棧溢出格式化爲鏈接。 –