ASP.NET跨页提交 获取GridView中的值 获取List泛型数据
将源页面 Source.aspx 中的内容提交到目标页面 Targe.aspx.提交页面是通过源页面
<asp:Button ID="Button1" runat="server" Text="提 交" PostBackUrl="~/Target.aspx" />实现.
问题:
1.源页面包含一个 GridView ,在目标页面想获取 GridView 中的所有数据.
GridView gvw = (GridView)PreviousPage.FindControl("GridView1");
GridView1 = gvw;
//这条输出语句是可以输出源页面 GridView 行数的.
Response.Write("Column Count: " + GridView1.Rows.Count.ToString());
//而这条语句却得不到 GridView 某行某列的数据(保证数据是存在的).
Response.Write("Cells Value: " + (GridView1.Rows[1].Cells[1].Text.ToString()));
//在源页面中可以获取 GridView 某行某列中 Label 绑定的数据,而目标页面中同样获取不到
Response.Write("Cells Value: " + (GridView1.Rows[1].Cells[1].FindControl("Label1") as Label).Text.ToString());
请问怎样才能获取到 GridView 中各行各列的数据呢?
2.获取源页面中的公共属性.属性类型是 List<Person>.源页面 Source.aspx.cs 相关代码:
protected List<Person> p;
//公共属性
public List<Person> Persons
{
get
{
return p;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Person p1 = new Person(1, "admin");
Person p2 = new Person(2, "cat");
Person p3 = new Person(3, "stone");
p = new List<Person>();
p.Add(p1);
p.Add(p2);
p.Add(p3);
}
}
//Person类
public class Person
{
private int id;
private string name;
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public Person() : this(0, string.Empty) { }
public Person(int id, string name)
{
this.id = id;
this.name = name;
}
}
目标页面 Target.aspx 相关代码:
在 Target.aspx 添加
<%@ Reference VirtualPath="~/source.aspx" %>
Target.aspx.cs 代码
//获取源网页公共属性的代码如下
Source sourcePage =(Source)PreviousPage;
string inputString = sourcePage.Persons[0].name;
Response.Write(inputString);
获取源页面公共属性时提示错误定位到 Target.aspx.cs ,说明 Source 缺少引用.这是哪里做错了呢?想要跨页获取 List 泛型 数据的方法怎么实现?
请高手不吝指教!谢谢!
补充:.NET技术 , ASP.NET