使用GridView将ArrayList数据打印到ASP.NET页面

时间:2017-11-05 06:27:48

标签: c# asp.net gridview

尝试将存储的arraylist数据打印到gridview。

.aspx档案,工作得很好。



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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Stock Management System</title>
    <style>
        body{
            background-color:#63AA9C;
            text-align:center
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table align="center" border="0" style="background-color:lightcoral">
        <tr><td>Item Name:</td><td><asp:TextBox ID="Name" placeholder="Item Name" runat="server"></asp:TextBox></td></tr>
        <tr><td>Quantity:</td><td><asp:TextBox ID="Quan"  placeholder="Quantity" runat="server"></asp:TextBox></td></tr>
        <tr><td>Price:</td><td><asp:TextBox ID="Pri" placeholder="Price" runat="server"></asp:TextBox></td></tr>
        <tr><td>Description:</td><td><asp:TextBox ID="Desc" placeholder="Descrition" runat="server"></asp:TextBox></td></tr>
        <tr><td><asp:Button ID="Save" runat="server" Text="Store Data" OnClientClick="Store()" width="200px"/></td></tr>
        <tr><td><asp:Button ID="Show" runat="server" Text="Show Data" OnClientClick="Print()" width="200px" /></td></tr>
        <tr><td><asp:Button ID="Delete" runat="server" Text="Remove Last Added Data" OnClientClick="Remove()" width="200px" /></td></tr>
        </table>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="LightCoral" ></asp:GridView>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="LightCoral" ></asp:GridView>
        <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="true" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="LightCoral"></asp:GridView>
        <asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="true" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="LightCoral"></asp:GridView>
    </div>
    </form>
</body>
</html>
&#13;
&#13;
&#13;

.aspx.cs文件

没有错误,但在按下show data按钮时无效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace WebApplication4
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        ArrayList items = new ArrayList();
        ArrayList price = new ArrayList();
        ArrayList quantity = new ArrayList();
        ArrayList description = new ArrayList();
        static int a = 0;
        protected GridView GridView1;
        protected GridView GridView2;
        protected GridView GridView3;
        protected GridView GridView4;

    protected void Page_Load(object sender, EventArgs e)
    {


    }
    public void Store()
    {
        items.Add(Name.Text);
        price.Add(Pri.Text);
        quantity.Add(Quan.Text);
        description.Add(Desc.Text);
        a++;
    }

    public void Print()
    {
        GridView1.DataSource = items;
        GridView1.DataBind();
        GridView2.DataSource = quantity;
        GridView2.DataBind();
        GridView3.DataSource = price;
        GridView3.DataBind();
        GridView4.DataSource = description;
        GridView4.DataBind();

    }

    public void Remove()
    {
        if (a > 0)
        {
            items.RemoveAt(a);
            a--;
        }
    }
}
}

我的页面,尝试显示存储的数据

enter image description here

1 个答案:

答案 0 :(得分:0)

关于您正在使用的OnClientClick属性,The documentation

  

获取或设置在引发Button控件的Click事件时执行的客户端脚本。

这意味着您正在尝试执行名为&#34; Print&#34;的JavaScript函数。在你的代码中(与其他按钮相同)。 你应该检查Button.Click事件。

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.click(v=vs.110).aspx