Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a usercontrol to add textbox controls dynamically. the property controlcount will decide how many textbox controls need to be added to the user control.

here is the code in user control

C#
public int controlcount
    {
        get
        {
            if (ViewState["controlcount"] != null)
                return (int)ViewState["controlcount"];
            return 4;
        }
        set
        {
            ViewState["controlcount"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }



    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        for (int i = 0; i < controlcount; i++)
        {
            TextBox t = new TextBox();
            t.ID = "textbox" + i.ToString();
            this.Controls.Add(t);
        }
    }



    public override void DataBind()
    {
        this.EnsureChildControls();
    }


in my aspx page i have added a button to increase the control count by 1. once the property is assigned databind is called. but controls is not added in first postback. but if another postback is fired then the controls will be added. this is happen because of CreateChildControls method in user control fire before the event of the button is fired.

here is the code in aspx


C#
protected void Page_Load(object sender, EventArgs e)
    {

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        DynamicInput1.controlcount += 1;
        DynamicInput1.DataBind();

    }


please suggest a solution to add controls when button click is fired without a additional postback.

thanks in advance
Posted
Updated 5-Nov-13 15:33pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900