Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

gridview - how to insert data in to grid view using asp.net without database

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this:

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();
    }
}

Hope it works!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...