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

Posted on 2008-01-25
Medium Priority
Last Modified: 2012-05-05
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 ID="newPaymentContent1">
        <asp:TableCell ColumnSpan="14" BackColor="#EFEEF7">
            <asp:Label ID="newPaymentSuccessLabel" runat="server"></asp:Label>&nbsp;
    <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 ColumnSpan="6" BackColor="#EFEEF7">
    <asp:TableRow ID="newPaymentSelectRow">
        <asp:TableCell ColumnSpan="2" BackColor="#EFEEF7">
            Brand:<asp:DropDownList ID="Brand1" runat="server" DataSourceID="BrandData" 
                DataTextField="BrandName" DataValueField="BrandID">
            <asp:SqlDataSource ID="BrandData" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MPOSConnectionString %>" 
                SelectCommand="EXEC [Abs].[GetBrandList]"></asp:SqlDataSource>
        <asp:TableCell ColumnSpan="3" BackColor="#EFEEF7">
            Product:<asp:DropDownList ID="Product1" runat="server" DataSourceID="ProductData" 
                DataTextField="ProductName" DataValueField="ProductID">
            <asp:SqlDataSource ID="ProductData" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MPOSConnectionString %>" 
                SelectCommand="EXEC [Abs].[GetProductList]"></asp:SqlDataSource>
        <asp:TableCell ColumnSpan="9" BackColor="#EFEEF7">
            GLCode:<asp:DropDownList ID="GLCode1" runat="server" DataSourceID="GLCodeData" 
                DataTextField="GLCodeName" DataValueField="GLCodeID">
            <asp:SqlDataSource ID="GLCodeData" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MPOSConnectionString %>" 
                SelectCommand="EXEC [Abs].[GetGLCodeList]"></asp:SqlDataSource>
    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();
        TableCell BrandCell = new TableCell();
        BrandCell.Attributes["Class"] = "cellstyle1";

Open in new window

Question by:ubya308
  • 2
LVL 26

Expert Comment

ID: 20751453
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)


Author Comment

ID: 20753787
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.

LVL 26

Accepted Solution

Rejojohny earned 1500 total points
ID: 20753967
>>Thanks - but I need the actual code to do this
thats what is given above ...

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question