Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
3.29/5 (3 votes)
See more:
Hi All

I have created dynamic controls with help of place holder and repeater but the controls are getting lost after postback...please can someone help me to resolve this.

Thanks
Posted
Updated 3-Dec-13 5:49am
v6
Comments
Ganesh KP 3-Dec-13 6:33am    
Writing such a huge code will not help you instead of that tell what do you want to achieve? Yes controls created dynamically will be lost after post back, you need to keep all the controls in a stack and after each post back again have to read from stack and add back to your UI that is the actual approach.

Hi
You wont be able to get the value of dynamic generated controls after postback.
You can store the values of the dynamic controls in Hidden field using Client Side scripting ( javascript )..
and you can access those values from hidden field in code behind.
 
Share this answer
 
ok you can do one thing on
C#
if (!IsPostBack)
           {
               string tripid = "11850"; //Request.QueryString["tripid"].ToString();
               getStage();
               QuestionsPopulate("Booking");

           }

You are already calling getStage();

now put else there

and

put following code in that

C#
drpStage.DataBind();
 
Share this answer
 
 
Share this answer
 
Comments
babli3 6-Dec-13 5:18am    
Thanks
C#
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            BindDataControl();
        }
        catch (Exception se)
        {
            //error if any
            Response.Write(se.Message);
        }
    }

    private void BindDataControl()
    {
        Repeater1.DataSource = GetData();
        Repeater1.DataBind();
    }

    //create dummy DataTable
    private DataTable GetData()
    {
        DataTable _dt = new DataTable();
        _dt.Columns.Add("Id", typeof(string));
        _dt.Columns.Add("Name", typeof(string));
        
        DataRow _drow = null;
        for (int i = 0; i < 5; i++)
        {
            _drow = _dt.NewRow();
            _drow[0] = i;
            _drow[1] = "Name" + i.ToString();
            _dt.Rows.Add(_drow);
        }
        _dt.AcceptChanges();

        return _dt;
    }
    
    public TextBox CreateDynamicControl(int id)
    {
        TextBox _txt = new TextBox();
        _txt.ID = "txt" + id.ToString();
        
        _txt.Height = new Unit(50);
        _txt.Width = new Unit(100);
        return _txt;
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
                Panel pnl = e.Item.FindControl("pnlCointainer") as Panel;
                TextBox t = new TextBox();
                t.ID = e.Item.ItemIndex.ToString();
                pnl.Controls.Add(t);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem ti in Repeater1.Items)
        {
            try
            {
                Panel pnl = ti.FindControl("pnlCointainer") as Panel;
                TextBox ttx = pnl.FindControl(ti.ItemIndex.ToString()) as TextBox;
                Response.Write("TxtId = " + ttx.ID + "Txt Text = " + ttx.Text);
            }
            catch (Exception se)
            {
                //error if any
                Response.Write(se.Message);
            }
        }
    }
 
Share this answer
 
v3
Comments
babli3 6-Dec-13 5:17am    
Thanks
use panel and then insert your Dynamic controls on the panel
using panels init() event.
 
Share this answer
 

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