我的代码背后有这段代码。
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();
}
}
我的视图中有四个文本框。当我点击提交按钮我的页面时,我试图在网格中显示这些数据。但我无法在网格中显示数据。
任何人都可以帮助我。答案 0 :(得分:1)
使用此:
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();
}
}
希望它有效!