Avatar of Devildib
Devildib
 asked on

Dynamically adding controls on asp.net webform

Hi experts,

I have a webform in which i am  displaying a label and a textbox in one section along with other controls.It looks like label above and textbox just below it.This combination of label and textbox is required by default.I need to dynamically give option to user for adding two more such label and textbox combinations just on the right of the default label and textbox.May be with two plus signs and some text written beside,clicking which will add the controls.Also some removal mechanism is required to remove the other two combinations of label and textboxes.On post, the controls should not be lost.
Please help.Better to have javascript and c# combination.The horizontal orientation of the controls is a must.
.NET ProgrammingC#JavaScript

Avatar of undefined
Last Comment
jorge_toriz

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Karrtik Iyer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jorge_toriz

You could use this simple code as a starting point.
<script src="../js/jquery-2.2.0.min.js"></script>
<style>
    .controlGroup {
        border-width: 1px;
        border-style: solid;
        float: left;
    }
</style>
<script>
    var Groups = {};
    Groups.controlCount = 1;
    Groups.addGroup = function () {
        if (Groups.controlCount == 3)
            alert('You can\'t add more groups');
        else {
            Groups.controlCount++;
            var htmlGroup = '<div class="controlGroup"><span id="lblLabel' + String(Groups.controlCount) + '">Label ' + String(Groups.controlCount) + '</span><br /><input type="text" id="txtTextBox' + String(Groups.controlCount) + '" name="txtTextBox' + String(Groups.controlCount) + '" /><input type="button" value="Add group" onclick="Groups.addGroup()" /></div>';
            $(htmlGroup).appendTo($('#controls'));
        }
    };
</script>
<div id="controls">
    <div class="controlGroup">
        <span id="lblLabel1">Label 1</span><br />
        <input type="text" id="txtTextBox1" name="txtTextBox1" />
        <input type="button" value="Add group" onclick="Groups.addGroup()" />
    </div>
</div>
<asp:Button ID="cmdSendInfo" runat="server" OnClick="cmdSendInfo_Click" Text="Send Info" />

Open in new window

protected void cmdSendInfo_Click(object sender, EventArgs e)
{
    List<string> text = new List<string>();
    text.Add(Request.Form["txtTextBox1"]);
    if (Request.Form["txtTextBox2"] != null)
    {
        text.Add(Request.Form["txtTextBox2"]);
        if (Request.Form["txtTextBox3"] != null)
        {
            text.Add(Request.Form["txtTextBox3"]);
        }
    }

    Response.Write(String.Join(" - ", text));
}

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck