We help IT Professionals succeed at work.

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

ubya308 asked
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

Watch Question

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

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.