goltu 0 Newbie Poster

I am able to succesful creating dynmically dropdownlist but i cannot use it seleted change event
Mainly problem

for (int i = 0; i < DDLCount; i++)
        {
            // Create the dropdownlists
            ddl = new DropDownList();
            la = new Label();
            //DropDownList dd2 = new DropDownList();
            ddl.ID = "Text" + i;
            la.ID = "la" + i;

            ddl.SelectedIndexChanged += new EventHandler(dd1_SelectedIndexChanged);
            //dd2.ID = "Texts" + i;
            LiteralControl l1 = new LiteralControl("<br></br>");

            
            SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
            con1.Open();
            string st = "select  doe_name from   Doe_detail ";
            SqlCommand cmd = new SqlCommand(st, con1);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ddl.Items.Add(dr.GetString(0).ToString());


            }
            con1.Close();
          
           

            // Add it to the panel
            Panel3.Controls.Add(ddl);
           Panel3.Controls.Add(la);
            Panel3.Controls.Add(l1);


        }
    }
    protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
    {

        SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
        con1.Open();
        string st = "select  doe_id from   Doe_detail  where doe_name=" + ddl.SelectedValue.ToString() + " ";
        SqlCommand cmd = new SqlCommand(st, con1);
        SqlDataReader dr = cmd.ExecuteReader();
        la.Text = dr.GetString(0).ToString();

}

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class assigned : System.Web.UI.Page
{
    DropDownList ddl;
    Label la;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
            con1.Open();
            string st = "select  Dep_name from   department ";
            SqlCommand cmd = new SqlCommand(st, con1);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                DropDownList1.Items.Add(dr.GetString(0).ToString());


            }
            con1.Close();

            if (IsPostBack && DDLCount > 0)
            {
                CreateDropDownLists();
            }

        }
    }
    public int DDLCount
    {
        get
        {
            // Try to get an instance of the DDLCount ViewState object
            object temp = ViewState["DDLCount"];
            // If temp is not null then cast it to int and return it,
            // otherwise return 0
            return temp == null ? 0 : (int)temp;
        }
        set { ViewState["DDLCount"] = value; }
    }
    private void CreateDropDownLists()
    {
        int a = Convert.ToInt32(TextBox4.Text);
        // This is just to set the count to a default value
        // You may possibly need to generate the count of dropdownlists
        // in some other manner, depending on your project
        //if (DDLCount == 0)
        DDLCount = a;   //i   have  to  ask  HOw  DDLCount  is  0
        // I  can  not   understand  it 


        for (int i = 0; i < DDLCount; i++)
        {
            // Create the dropdownlists
            ddl = new DropDownList();
            la = new Label();
            //DropDownList dd2 = new DropDownList();
            ddl.ID = "Text" + i;
            la.ID = "la" + i;

            ddl.SelectedIndexChanged += new EventHandler(dd1_SelectedIndexChanged);
            //dd2.ID = "Texts" + i;
            LiteralControl l1 = new LiteralControl("<br></br>");

            
            SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
            con1.Open();
            string st = "select  doe_name from   Doe_detail ";
            SqlCommand cmd = new SqlCommand(st, con1);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ddl.Items.Add(dr.GetString(0).ToString());


            }
            con1.Close();
          
           

            // Add it to the panel
            Panel3.Controls.Add(ddl);
           Panel3.Controls.Add(la);
            Panel3.Controls.Add(l1);


        }
    }
    protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
    {

        SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
        con1.Open();
        string st = "select  doe_id from   Doe_detail  where doe_name=" + ddl.SelectedValue.ToString() + " ";
        SqlCommand cmd = new SqlCommand(st, con1);
        SqlDataReader dr = cmd.ExecuteReader();
        la.Text = dr.GetString(0).ToString();
        
    }




    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CreateDropDownLists();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string value = "";
        string ids = "";
        // Cycle through each dropdownlist
        for (int i = 0; i < DDLCount; i++)
        {
            // Find the dropdownlist inside the Panel
            DropDownList ddl = Panel3.FindControl("Text" + i) as DropDownList;

            // Set the label to the SelectedValue of the dropdownlist
            //Label1.Text += ddl.SelectedValue + " : ";
            value = ddl.SelectedValue.ToString();
            //DropDownList dd2 = Panel3.FindControl("Texts" + i) as DropDownList;

            // Set the label to the SelectedValue of the dropdownlist
            //Label1.Text += ddl.SelectedValue + " : ";
            //ids = dd2.SelectedValue.ToString();
            //SqlConnection con = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");

            //con.Open();

            //SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "','" + ids + "')", con);
            //cmd1.ExecuteNonQuery();
            //con.Close();
        }
    }
    //public int refresh
    //{
    //    get
    //    {
    //        // Try to get an instance of the DDLCount ViewState object
    //        object temp = ViewState["refresh"];
    //        // If temp is not null then cast it to int and return it,
    //        // otherwise return 0
    //        return temp == null ? 0 : (int)temp;
    //    }
    //    set { ViewState["refresh"] = value; }
    //}
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (ViewState["DDLCount"] != null)
        {
            
                Panel3.Controls.Clear();
                ViewState["DDLCount"] = null;
           

        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.