1

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?

1
  • Does the new textbox erase the previous one or overlap it? Commented Jan 20, 2014 at 16:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

THANKS ALOT... but i still have a problem, at the part div.Controls.Add(new TextBox()); i put this instead: >>>>>>>> TextBox tb = new TextBox();div.Controls.Add(tb);tb.text=TextBox2.text; where textbox2 is a temporary textbox to hold text and then put it in the generated new tb in div....the problem is when i create first tb with a text, the second created tb's text changes the first one and so on ...how can i solve it?
Yes, it's because when you assign the Text of the First Textbox to the second Textbox, it's passing the reference of the First Textbox to the second one. In order to prevent that, we need to clone the value. Instead of assigning tb.Text = TextBox2.Text;, you could assign it by tb.Text = TextBox2.Text.Clone(); Hope it helps.
Thanks for the reply again, but i get the error of "Cannot implicitly convert type "object" to string ..."
How about this: tb.Text = TextBox2.Text.Clone().ToString();
about that, Again the same text gets copy to all the previous text boxes like before
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.