How can I clone some form elements and them refer to them in a c# script?

I am building a form to input payment information. I would like a facility to add additional form fields(a row of input or select fields) on the click of a button and then refer to them within a C# script.

I am more than able to clone the rows I need, but I can't refer to them in any script that works, so I am now thinking that I am cloning or adding the elements incorrectly.

Below is what the form looks like.

I have also included the code which I am using to insert a new form element. The problem is I can't seem to refer to the new element when the submit button is clicked.

Do I insert the new field in a different way or can I simply refer to the new field in some way?

How can I do this?
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <form id="form1" runat="server">
    <asp:Table ID="newPaymentTable" runat="server">
    
    <asp:TableRow ID="newPaymentHeader" Height="25px">
        <asp:TableCell ColumnSpan="14" BackColor="#E2E0F6">&nbsp;</asp:TableCell>
    </asp:TableRow>
    <asp:TableRow ID="newPaymentContent1">
        <asp:TableCell ColumnSpan="14" BackColor="#EFEEF7">
            <asp:Label ID="newPaymentSuccessLabel" runat="server"></asp:Label>&nbsp;
        </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow ID="newPaymentInsertRowButton">
        <asp:TableCell ColumnSpan="8" BackColor="#EFEEF7">
            <asp:Button ID="NewRowButton" runat="server" onclick="NewRowButton_Click" Text="Add New Element" />
            <asp:Button ID="FormSubmitButton" runat="server" onclick="FormSubmitButton_Click" Text="Submit" />
        </asp:TableCell>
        <asp:TableCell ColumnSpan="6" BackColor="#EFEEF7">
            &nbsp;
        </asp:TableCell>
    </asp:TableRow>
    
    <asp:TableRow ID="newPaymentSelectRow">
    
        <asp:TableCell ColumnSpan="2" BackColor="#EFEEF7">
            Brand:<asp:DropDownList ID="Brand1" runat="server" DataSourceID="BrandData" 
                DataTextField="BrandName" DataValueField="BrandID">
            </asp:DropDownList>
            <asp:SqlDataSource ID="BrandData" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MPOSConnectionString %>" 
                SelectCommand="EXEC [Abs].[GetBrandList]"></asp:SqlDataSource>
        </asp:TableCell>
        
        <asp:TableCell ColumnSpan="3" BackColor="#EFEEF7">
            Product:<asp:DropDownList ID="Product1" runat="server" DataSourceID="ProductData" 
                DataTextField="ProductName" DataValueField="ProductID">
            </asp:DropDownList>
            <asp:SqlDataSource ID="ProductData" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MPOSConnectionString %>" 
                SelectCommand="EXEC [Abs].[GetProductList]"></asp:SqlDataSource>
        </asp:TableCell>
        
        <asp:TableCell ColumnSpan="9" BackColor="#EFEEF7">
            GLCode:<asp:DropDownList ID="GLCode1" runat="server" DataSourceID="GLCodeData" 
                DataTextField="GLCodeName" DataValueField="GLCodeID">
            </asp:DropDownList>
            <asp:SqlDataSource ID="GLCodeData" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MPOSConnectionString %>" 
                SelectCommand="EXEC [Abs].[GetGLCodeList]"></asp:SqlDataSource>
        </asp:TableCell>
        
    </asp:TableRow>
    
    </asp:Table>
 
</form>
 
    </asp:Content>
 
 
====================================================================
 
    protected void NewRowButton_Click(object sender, EventArgs e)
    {
        DropDownList NewDropDownBrandList = new DropDownList();
        NewDropDownBrandList.ID = "Brand2";
        NewDropDownBrandList.DataSourceID = "BrandData";
        NewDropDownBrandList.DataTextField = "BrandName";
        NewDropDownBrandList.DataValueField = "BrandID";
 
        TableRow tRow = new TableRow();
        newPaymentTable.Rows.Add(tRow);
 
        TableCell BrandCell = new TableCell();
        BrandCell.Attributes["Class"] = "cellstyle1";
        BrandCell.Controls.Add(NewDropDownBrandList);
 
 
        tRow.Cells.Add(BrandCell);
    }

Open in new window

ubya308Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RejojohnyCommented:
be aware that when you add dynamic controls the way you are planning to do, the page does not reemeber the previously created controls whenever there is a postback .. so you need to create the same controls in the page load or page_init so that evertime there is a postback, the controls are created again ..

to access a dynamic control .. you will have to use code like
page.findcontrol("ControlId") .. in your sample code to get the values of rht edropdownlist .. try

Dim lBrand as Dropdownlist = ctype(page.findcontrol("Brand2"), Dropdownlist)

Thanks,
Rejo
0
ubya308Author Commented:
Thanks - but I need the actual code to do this. I have tried about a thousand different ways to get it to work and none of them do.

M
0
RejojohnyCommented:
>>Thanks - but I need the actual code to do this
thats what is given above ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.