我有一个类对象列表。
[Serializable]
public class UserRequestOrders
{
private List<RTORequestOrder> m_thisUsersOrders = new List<RTORequestOrder>();
public List<RTORequestOrder> RequestOrders
{
get { return m_thisUsersOrders; }
set { m_thisUsersOrders = value; }
}
}
当我创建此对象的实例时,我需要填充list变量当m_thisUsersOrders具有现有的请求列表(来自viewstate)。
MyOrders.RequestOrders = (List<RTODataEntryObjects.RTORequestOrder>)ViewState["vsMyOrders"];
当页面回发时,我将视图状态转换为RTORequestOrder对象列表,并尝试设置RequestOrders属性,我得到无法分配属性或索引器的消息。我有一个“SET”访问器属性。
我在这个主题上阅读的所有帖子都说明我需要在应用程序设置中修改范围,但我不确定这在哪里适用。我没有设置任何应用程序设置。
我希望有人可以帮助我了解如何从现有列表中填充列表。
编辑:Nico,感谢您的回复!以下是代码隐藏页面的完整代码。 “MyOrders”和“lst”基本上是一回事。当我使用“lst”时,一切都按照我想要的方式工作。我想要使用“MyOrders”对象的原因是因为我有一个方法将列表作为数据表返回,只有我需要显示的字段。 (我没有'显示该代码,因为问题似乎与“SET”访问器有关。)“lst”具有与“MyOrders.RequestOrders”相同的签名。为什么我可以将viewstate转换为lst对象,而不是MyOrders对象? 编辑:格兰特,谢谢你的回复。我不知道如何为ASP页面设置断点...... :(public partial class Default3 : System.Web.UI.Page
{
RTODataEntryObjects.UserRequestOrders MyOrders = new RTODataEntryObjects.UserRequestOrders();
List<RTODataEntryObjects.RTORequestOrder> lst = new List<RTODataEntryObjects.RTORequestOrder>();
void Page_PreRender(object sender, EventArgs e)
{
if (ViewState["vsMyOrders"] == null)
{
NewDataGrid.DataSource = (RTODataEntryObjects.UserRequestOrders)ViewState["vsMyOrders"]; // code to show the data in the grid when page loading
}
}
protected void Page_Load(object sender, EventArgs e)
{
NewDataGrid.EnableViewState = true;
NewDataGrid.DataSource = (RTODataEntryObjects.UserRequestOrders)ViewState["vsMyOrders"];
NewDataGrid.DataBind();
}
public void btnSubmit(object sender, EventArgs e)
{
int id = Int32.Parse(TextBox1.Text); // dynamically getting the data from the frontend.
string name = TextBox2.Text; // dynamically getting the data from the frontend.
if (ViewState["vsMyOrders"] != null) // if the view state is already having data then can update the list and assign again to the viewstate later.
{
lst = (List<RTODataEntryObjects.RTORequestOrder>)ViewState["vsMyOrders"];
MyOrders.RequestOrders = (List<RTODataEntryObjects.RTORequestOrder>)ViewState["vsMyOrders"];
}
RTODataEntryObjects.RTORequestOrder thisOrder = new RTODataEntryObjects.RTORequestOrder(id, name, User.Identity.Name, System.Environment.MachineName);
lst.Add(thisOrder); //
MyOrders.AddNew(thisOrder);
ViewState["vsMyOrders"] = MyOrders;
NewDataGrid.DataSource = (IList<RTODataEntryObjects.RTORequestOrder>)ViewState["vsMyOrders"];
NewDataGrid.DataBind();
}
}