ASP.NET - Dynamic Controls - Asked By Justin Deva on 13-Aug-12 05:50 AM


  

Dynamic Dropdownlist disappears when its selected index changed???

wat s the Problem...suggest pls

ajay chauhan replied to Justin Deva on 13-Aug-12 06:49 AM
Hello,

Have u done this as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString());
        DataTable odt = new DataTable();
        DropDownList ddl = new DropDownList();
        try
        {
            sqlCon.Open();
            SqlDataAdapter sqlda = new SqlDataAdapter("select * from cState", sqlCon);
            sqlda.Fill(odt);
            ddl.DataSource = odt;
            ddl.DataTextField = "StateName";
            ddl.DataValueField = "StateId";
            ddl.DataBind();
            PlaceHolder1.Controls.Add(ddl);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }
}

thanks,
Ajay
Peter Bromberg replied to ajay chauhan on 13-Aug-12 08:05 AM
dynamically created controls must be re-created whenever there is a postback, because they were not originally a part of the page markup.