user3215839
user3215839

Reputation: 35

Visual 2012 C# ASP.net Add programmatically control under control

I'm designing in a web form.. the idea is like to design a forum -Like Here - where there is a main post body (Like this question im writing) and there be some replies to this question (like The ones will be answered to this question).

I have problem in putting reply comments under each other

I have made a reply button, when it is pressed,this code executes:

TextBox tb = new TextBox();
Panel3.Controls.Add(tb);

which panel3 is a panel under the main body, and textBox is a field for reply comments, and i want there to be as many as text boxes in the panel3 as the button is inserted.

when i click for the 2nd or 3rd or.... times, the current text box goes over the previous one and it wont go under it

how can i position each under previous text box?

Upvotes: 1

Views: 338

Answers (1)

Hatjhie
Hatjhie

Reputation: 1365

This is related to CSS or HTML issues.

The idea is to add <div> to contain the TextBox before they are added to the panel.

Below are the sample codes:

 public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["Counter"] = 0;
        }
        else
        {
            //Sync ViewState Info to maintain the state
            List<string> texts = new List<string>();

            //The dynamic textbox values are captured from Request.Form
            foreach (string key in Request.Form.Keys)
            {
                if (key.Contains("ctrlsuper"))
                {
                    texts.Add(Request.Form[key]);
                }
            }
            Texts = texts;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //Rerender the textboxes to UI and add one more new empty textbox.
        for (int i = 0; i <= Convert.ToInt32(ViewState["Counter"]); i++)
        {
            HtmlGenericControl div = new HtmlGenericControl("div");
            TextBox tb = new TextBox();
            tb.ID = "ctrlsuper" + i.ToString();

            //Refresh the textbox text according to its previous value.
            if (Texts.Count > 0 && i < Texts.Count)
            {
                tb.Text = Texts[i];
            }

            div.Controls.Add(tb);
            pnlControls.Controls.Add(div);
        }

        ViewState["Counter"] = Convert.ToInt32(ViewState["Counter"]) + 1;
    }

    public List<string> Texts
    {
        get
        {
            if (ViewState["Texts"] == null)
            {
                return new List<string>();
            }
            else
            {
                return ViewState["Texts"] as List<string>;
            }
        }
        set
        {
            ViewState["Texts"] = value;
        }
    }
}

The ASPX:

<asp:Panel runat="server" ID="pnlControls">
    </asp:Panel>

    <asp:Button runat="server" ID="btnAdd" Text="Add" OnClick="btnAdd_Click" />

Hope it helps.

Upvotes: 1

Related Questions