tags:

views:

496

answers:

3

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. Then for the next line, I just repeat the controls, but add a 2 to the end of the Id. This goes on with 3, 4, 5, etc up until however many line items I need. 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?

+1  A: 

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

Ultimately, it depends how easily you would like to be able to access the value of those 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.

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.

Loren
+1  A: 

The simplest way is to override the pages OnInit function, and add the controls there.

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)

Andrew Shepherd
A: 

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. 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.

iamdudley
That is Correct!
Xaisoft
If I have time I will post some code to do this but at the moment I'm pretty busy. Hopefully you should be able to find plenty of examples about using datagrids.
iamdudley