2016-04-26 111 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

我无法创建列表的人口。因为我正在学习,所以需要着眼于如何实现这一目标。我尽快发布结果。非常感谢您的回答:) –