简体   繁体   English

在运行时为ASP.NET Invoice App创建ASP.NET控件?

[英]Creating ASP.NET controls at runtime for an ASP.NET Invoice App?

I am creating a web app that will be used to create invoices and quotes. 我正在创建一个用于创建发票和报价的网络应用。 Currently, I am tediously creating all the controls at design time resulting in controls like the following: 当前,我在设计时就很繁琐地创建所有控件,导致产生如下控件:

txtQty1, txtDescription1, txtUnitPrice1, txtItemCode1. txtQty1,txtDescription1,txtUnitPrice1,txtItemCode1。 Then for the next line, I just repeat the controls, but add a 2 to the end of the Id. 然后对于下一行,我只是重复控件,但是在ID的末尾添加2。 This goes on with 3, 4, 5, etc up until however many line items I need. 继续进行3、4、5等,直到我需要许多订单项为止。 I know this is a stupid way of doing it and I should probably do it at runtime, but I am not sure what is the best way to do it at runtime and could I possibly use JQuery to add the ASP.NET controls at runtime? 我知道这是一种愚蠢的方式,我可能应该在运行时执行,但是我不确定在运行时执行此操作的最佳方法是什么,我是否可以在运行时使用JQuery添加ASP.NET控件?

It sounds like you are wanting to dynamically create some number of controls at run time and add them to the page. 听起来您好像想在运行时动态创建一些控件并将其添加到页面中。 This is fairly common, but unfortunately is much more complicated. 这是相当普遍的,但不幸的是要复杂得多。 Everything from referencing the controls to adding events becomes just a little bit harder. 从引用控件到添加事件的所有操作都变得有点困难。 Additionally, dynamically added controls must be re-added during each postback. 此外,必须在每次回发期间重新添加动态添加的控件。 The controls themselves do not automatically persist between postbacks. 控件本身不会在回发之间自动保留。

This article gives a great introduction to the topic: http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx 本文对该主题进行了很好的介绍: http : //www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx

Ultimately, it depends how easily you would like to be able to access the value of those TextBoxes. 最终,这取决于您希望能够多么容易地访问那些TextBoxes的值。 In JQuery you can create textboxes on the client side, but it will be even harder to use those values from the ASP.NET code-behind page, because they won't be server-side controls. 在JQuery中,您可以在客户端上创建文本框,但是使用ASP.NET代码隐藏页中的这些值将更加困难,因为它们不会成为服务器端控件。

If it is just going to be a few controls, I would recommend sticking a few controls on during design time and showing and hiding them, as you are already doing. 如果只是一些控件,我建议您在设计时坚持使用一些控件,并像已经做的那样显示和隐藏它们。 When you are dealing with more than a few, potentially many, repeated controls (which it sounds like you might be), you may have to go with dynamically added controls. 当您处理多个(可能很多)重复的控件(听起来可能像是)时,可能必须使用动态添加的控件。 Again, I tend to avoid this route unless I have to, because it adds a great deal of complexity. 同样,除非有必要,否则我倾向于避免使用此路线,因为它增加了很多复杂性。

The simplest way is to override the pages OnInit function, and add the controls there. 最简单的方法是覆盖页面的OnInit函数,并在其中添加控件。

eg 例如

protected override void OnInit(EventArgs e)
{
    for (int i = 0; i != 10; ++i)
    {
        TextBox tb = new TextBox();
        // Init textBox here
        Label lb = new Label();
        // Init Label here

        Literal lt = new Literal();
        lt.Text = "<br />";
        this.Form.Controls.Add(tb);
        this.Form.Controls.Add(lb);
        this.Form.Controls.Add(lt);
    }
}

You can also investigate databound controls (eg GridView, ListView and Repeater) 您还可以调查数据绑定控件(例如GridView,ListView和Repeater)

I'm guessing you are trying to write a "Create Invoice" page where a user can add an unlimited number of line items to the invoice. 我猜您正在尝试编写“创建发票”页面,用户可以在其中添加无限数量的订单项到发票。

So you need a bunch of controls to enter the invoice data (like UserId, Date, Time, CustomerId, etc), and then a datagrid to enter the line items. 因此,您需要一堆控件来输入发票数据(如UserId,Date,Time,CustomerId等),然后需要一个datagrid来输入订单项。 This datagrid has an " Add new row (line item) " button. 该数据网格具有“ 添加新行(订单项) ”按钮。 The user enters the line item details and then clicks the button to add another line item. 用户输入订单项详细信息,然后单击按钮以添加另一个订单项。 Or submits the completed invoice. 或提交完整的发票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM