简体   繁体   English

仅使用列表/集合将数据本地存储在网页上

[英]Use only list/collection to store data locally on webpage

I'm getting only 1 record using this code, but i want to display multiple records on page. 我使用此代码仅获得1条记录,但我想在页面上显示多条记录。 I have 3 columns to display on page which are: id , name and lastname . 我在页面上显示3列: idnamelastname How can I get this done? 我该怎么做?

Code Behind: 背后的代码:

protected List<Class1> GetClass1()
{
    Class1 uinfo = new Class1();
    uinfo.ID = Convert.ToInt16(TextBox1.Text);
    uinfo.Name = TextBox2.Text;
    uinfo.LastName = TextBox3.Text;
    data.Add(uinfo);
    return data;   
}

protected void BindUserDetails()
{
    data = GetClass1();
    GridView1.DataSource = data;
    GridView1.DataBind();
}

Class file: 类文件:

public class Class1
{
    Int16 id;
    string name = string.Empty;
    string lastname = string.Empty;

    public Int16 ID
    {
        get { return id; }
        set { id = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string LastName
    {
        get { return lastname; }
        set { lastname = value; }
    }
}

I got one solution for you. 我为您提供了一种解决方案。 This is just a sample implementation. 这只是一个示例实现。 Please modify according your requirement. 请根据您的要求进行修改。

The Code : 编码 :

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

namespace BindingListSample
{
    public partial class _Default : System.Web.UI.Page
    {
        static List<Employee> bindingL = new List<Employee>();
        protected void Btn_Click(object sender, EventArgs e)
        {
            bindingL.Add(new Employee { Name = TxtName.Text });
            GrvSample.DataSource = bindingL;
            GrvSample.DataBind();
        }
    }
    public class Employee
    {
        public string Name { get; set; }
    }
}

The problem is that you need to use static with your list. 问题是您需要对列表使用static。 When you use static you can store the values that you have inserted until the application is closed. 使用static时,您可以存储在应用程序关闭之前插入的值。 Refer Static Keyword for more explanation. 请参阅静态关键字以获取更多说明。

This is just my way to tackle this situation. 这只是我解决这种情况的方法。

Hope This Helps 希望这可以帮助

 `List<Class1> data = new List<Class1>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
        if (Session["test"] != null)
        {
            data = Session["test"] as List<Class1>;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Class1 cl = new Class1 { ID = Convert.ToInt16(TextBox1.Text), Name = TextBox2.Text, LastName = TextBox3.Text };
        data.Add(cl);            
        Session["test"] = data;

        dataBind();
    }

    protected void dataBind()
    {
        GridView1.DataSource = data;
        GridView1.DataBind();
    }
}

}` }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM