2016-04-26 114 views
0

我正在嘗試製作一個項目,將網站的源代碼作爲文本區域的輸入,然後搜索以查找該網頁中的元素數量。我能夠搜索並將輸出作爲警報彈出窗口。我希望輸出在網格視圖或數據表中,而不是警報彈出窗口。 主頁代碼:如何將數據添加到Gridview?

<body> 

    <textarea id="TextArea1"></textarea> 
    <br /> 
    <input id="Submit1" type="submit" value="submit" /></div> 

    <script> 
       var $textarea = $('#TextArea1'), $submit = $('#Submit1'); 
      $submit.click(function (e) { 
        e.preventDefault(); 
        sourceCode = $textarea.val(); 
        var $searchObject = $('<div id="Searching"></div>'); 
        $searchObject.append($(sourceCode)); 

     alert("Number of text boxes = " + $searchObject.find('[type=text]').length); 
        $searchObject.find('[type=text]').each(function() { 
        alert("Name of textbox = " + $(this).attr("name") + " and its ID is " + $(this).attr("id")); 
     }); 

     alert("Number of Submit Buttons = " + $searchObject.find('[type=submit]').length); 
        $searchObject.find('[type=submit]').each(function() { 
        alert("Name of Submit button = " + $(this).attr("name") + " and its ID is =" + $(this).attr("id")); 
     }); 
    </script> 

<form runat="server"> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="178px" Width="1191px"> 
    <Columns> 
    <asp:BoundField HeaderText="Element" DataField="Element"/> 
    <asp:BoundField HeaderText="Element Name" DataField="Element name"/> 
    <asp:BoundField HeaderText="Element ID" DataField="Element ID"/> 
    </Columns> 
    </asp:GridView> 
    <asp:Button ID="Button1" runat="server" Text="WebElements.xlsx" /> 
</form> 

</body> 
</html> 

後端代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
namespace Generic_GUI 
{ 
    public partial class HomePage : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!this.IsPostBack) 
      { 
       DataTable dt = new DataTable(); 
       dt.Columns.AddRange(new DataColumn[4] { 
          new DataColumn("Element",typeof(string)), 
          new DataColumn("Element Name",typeof(string)), 
          new DataColumn("Element ID",typeof(string)), 

       dt.Rows.Add("", "", "", ""); 
       dt.Rows.Add("", "", "", ""); 
       dt.Rows.Add("", "", "", ""); 
       GridView1.DataSource = dt; 
       GridView1.DataBind(); 
      } 
     }  
    } 
} 

任何人都可以幫助我的代碼轉移出來放到網格視圖,而不是警告彈出窗口?我之前沒有和GridView一起工作過,所以不知道如何實現這一點。

回答

0

創建一個類來保存結果

public class Element 
    { 
     public string Type { get; set; } 
     public string Name { get; set; } 
     public string ID { get; set; } 

     public Element(string type = "", string name = "", string id = "") 
     { 
      this.Type = type; 
      this.Name = name; 
      this.ID=id; 
     } 
    } 

創建一個IEnumerable容器,以便容納

public List<Element> ListOfElements = new List<Element>(); 

    // Example population of List<> 
    Element el; 

    el = new Element("Type1", "Name1", "ID1"); 
    ListOfElements.Add(el); 

    el = new Element("Type2", "Name2", "ID2"); 
    ListOfElements.Add(el); 

填充列表將其綁定到GridView

GridView1.DataSource = ListOfElements ; 
GridView1.DataBind(); 

GridView控件後即可在標記中定義爲:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> 
    <Columns> 
     <asp:BoundField DataField="Type" HeaderText="Element Type" SortExpression="Type"/> 
     <asp:BoundField DataField="Name" HeaderText="Element Name" SortExpression="Name"/> 
     <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"/> 
    </Columns> 
</asp:GridView> 
+0

我無法創建列表的人口。因爲我正在學習,所以需要着眼於如何實現這一目標。我儘快發佈結果。非常感謝您的回答:) –