ASP.NET 动态控件添加
原始帖子
对于我的特定问题,有很多大量文章,而且问题比此处发布的答案还要多。
我正在创建一个网页,其中包含一个产品的 DropDownList 控件,用户可以从中选择产品以输入订单进行处理。除此之外,还有两 (2) 个其他 DropDownList 控件,其中包含每个特定产品的选项和附件。我希望用户能够将多个产品行输入到订单对象中,以便与相应的选项和/或*配件一起进行处理。这将减少数据库中存储的客户信息记录量。
当用户从此 DropDownList 中进行选择时,我想创建一个产品列表,但必须根据用户必须的产品数量动态创建该列表输入特定订单。我理解保存数据所涉及的需求,因为它不会自动保存到 ViewState,但无法弄清楚如何将数据推向正确的方向。
由于处理 ViewState 的复杂性,我是否应该使用 ASP.NET 以外的其他工具来创建网页?虽然可能很复杂,但在尝试采取这种行动时是否有更好的方法可以使用?
编辑
所以我的发现让我相信,为了在添加数据时正确存储数据,我应该使用 Repeater 控件并将其 DataBind 到包含 KeyValuePairs 中的数据的 Dictionary 对象。我正在寻找一种简化的方法来执行此操作,而不是直接添加一个包含我需要的所有数据的对象。我当然可以将此类添加到 Repeater 中,并将 DataBind 添加到其中,但是从 Repeater 中重新恢复数据会给我带来很多问题。
public class Order
{
public Order() { }
public string CustomerNamer { get; set; }
public ProductList ProductOrdered { get; set; }
}
public class ProductList : List<Product>
{
public ProductList() { }
}
public class Product
{
public Product() { }
public int ID { get; set; }
public string Name { get; set; }
public string ProdcutCode { get; set; }
}
背后代码:
protected void order_add_Click(object sender, EventArgs e)
{
Dictionary<int, Order> objOrders = new Dictionary<int, Order>();
Order objOrder = new Order();
Product objProduct = new Product();
objProduct.Name = "Tire";
objProduct.ProductCode = "75R15";
objOrder.ProductsOrdered = new ProductList();
objOrder.ProductsOrdered.Add(objProduct);
objOrders.Add(0, objOrder);
rptTest.DataSource = objOrders;
rptTest.DataBind();
}
ORIGINAL POST
There are a ton of articles out there and a lot more questions than answers posted here on SO for my particular question.
I have a web page that I'm creating that will have a DropDownList control of products a user can select from to enter into an order for processing. In addition to that there are two (2) other DropDownList controls that contain options and accessories for each particular product. I want the user to be able to enter in multiple product lines into the order object for processing along with corresponding options and/or *accessories. This will reduce the amount of records to store for customer information within the database.
As the user is selecting from this DropDownList I would like to create a list of products but this list will have to be created dynamically based upon the number of products the user has to enter for a specific order. I understand the needs involved of saving the data as it is not automatically saved to the ViewState but having trouble figuring out how to push the data in the right direction.
Should I be looking to use something other than ASP.NET to create my web page due to the complexities of dealing with the ViewState? While it may be complex, is there a better methodology to use when attempting such a course of action?
EDIT
So my findings are bringing me to believe that in order to store the data properly as it is being added I should be using a Repeater control and DataBind that to a Dictionary object containing my data in KeyValuePairs. I'm looking for a simplified way of doing this to instead just add directly an object that contains all data I would need. I can certainly add this class to the Repeater and DataBind to it but re-hydrating the data back out of the Repeater is causing me more than a few issues.
public class Order
{
public Order() { }
public string CustomerNamer { get; set; }
public ProductList ProductOrdered { get; set; }
}
public class ProductList : List<Product>
{
public ProductList() { }
}
public class Product
{
public Product() { }
public int ID { get; set; }
public string Name { get; set; }
public string ProdcutCode { get; set; }
}
CODE BEHIND:
protected void order_add_Click(object sender, EventArgs e)
{
Dictionary<int, Order> objOrders = new Dictionary<int, Order>();
Order objOrder = new Order();
Product objProduct = new Product();
objProduct.Name = "Tire";
objProduct.ProductCode = "75R15";
objOrder.ProductsOrdered = new ProductList();
objOrder.ProductsOrdered.Add(objProduct);
objOrders.Add(0, objOrder);
rptTest.DataSource = objOrders;
rptTest.DataBind();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于通过渲染页面中的Repeater 表示的动态集合,我已多次成功使用此模式:
创建可以表示信息的内存中结构。
将此结构公开为页面上的属性,并根据需要在内部将其存储在
Session
或ViewState
中。公开列表<订单>订单
{
获取 { 返回 ViewState("OrdersList"); }
设置 { ViewState("OrdersList") = value; }
}
DataBind()
到转发器,并让绑定将内存中的对象转换为呈现时的样子。回发时,从中继器读取任何更改并更新内存中的数据结构(在
Page_Load
处理程序中 ->if (!IsPostback) {...}
)。当需要将选择保留到数据库(或提交订单等)时,只需从内存存储中读取信息即可。
For dynamic collections which are represented via a
Repeater
in the rendered page I've successfully used this pattern many times:Create an in-memory structure that can represent the information.
Expose this structure as a property on the page, and internally store it in the
Session
orViewState
as appropriate.public List<Order> Orders
{
get { return ViewState("OrdersList"); }
set { ViewState("OrdersList") = value; }
}
DataBind()
to the repeater and have the binding transform your in-memory object to whatever it should look like when rendered.On post-back, read any changes from the repeater and update your in-memory data structure (in
Page_Load
handler ->if (!IsPostback) {...}
).When it's time to persist selection to DB (or submit the order, etc.) simply read info from your in-memory store.