【问题标题】:How to add data to a Gridview?如何将数据添加到 Gridview?
【发布时间】:2016-04-26 05:13:11
【问题描述】:

我正在尝试制作一个项目,该项目通过文本区域将网站的源代码作为输入,然后搜索以查找该页面中的元素数量。我能够搜索并将输出作为警报弹出窗口。我希望输出位于网格视图或数据表中,而不是弹出警报。 主页代码是:

<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,所以不知道如何实现这一点。

【问题讨论】:

    标签: jquery html asp.net gridview


    【解决方案1】:

    创建一个类来存储结果

        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>
    

    【讨论】:

    • 我在创建列表人口时遇到问题。这是因为我边走边学,需要研究如何实现这一目标。我一完成就发布结果。非常感谢您的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多