如何在没有数据库的情况下使用asp.net将数据插入到网格视图中 [英] how to insert data in to grid view using asp.net without database

查看:19
本文介绍了如何在没有数据库的情况下使用asp.net将数据插入到网格视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码后面有这段代码.

I have this code in my code behind.

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public class fields
    {
        public string firstname { get; set; }
        public string middlename { get; set; }
        public string lastname { get; set; }
        public string phonenumber { get; set; }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<fields> data = new List<fields>();

        string fn = TextBox1.Text;
        string mn = TextBox3.Text;
        string ln = TextBox2.Text;
        string pn = TextBox4.Text;

        fields s = new fields();
        s.firstname = TextBox1.Text;
        s.middlename = TextBox3.Text;
        s.lastname = TextBox2.Text;
        s.phonenumber = TextBox4.Text;

        data.Add(s);

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



    }
}

我有四个文本框.当我点击提交按钮我的页面时,我试图在网格中显示这些数据.但我无法在网格中显示数据.

I have four textboxes in my view. I am trying to show in the grid those data when I click submit button my page. But I am not able to show the data in the grid.

有没有人可以帮我一下.

can any body help me out.

推荐答案

使用这个:

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

[Serializable]
public class fields
{
    public string firstname { get; set; }
    public string middlename { get; set; }
    public string lastname { get; set; }
    public string phonenumber { get; set; }

}

public partial class _Default : System.Web.UI.Page
{
    List<fields> data = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        data = ViewState["_data"] as List<fields>;
        if (data == null) 
            data = new List<fields>();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string fn = TextBox1.Text;
        string mn = TextBox3.Text;
        string ln = TextBox2.Text;
        string pn = TextBox4.Text;

        fields s = new fields();
        s.firstname = TextBox1.Text;
        s.middlename = TextBox3.Text;
        s.lastname = TextBox2.Text;
        s.phonenumber = TextBox4.Text;

        data.Add(s);
        ViewState["_data"] = data;

        GridView1.DataSource = data;
        GridView1.DataBind();
    }
}

希望它有效!

这篇关于如何在没有数据库的情况下使用asp.net将数据插入到网格视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆