Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is when I clicked on one listbox another list box has to come dynamically with related values.... list box is coming but listbox event is not firing....I saw some answers in this website but that answer is not suited for my application can any one please give the answer... Here is a quick code please see the code and give me the answers asap...

Thanks in advance....

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

            SqlCommand com = new SqlCommand("select EmpId,EmpName from OrgChart where      ManagerID=0", con);
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
           int i=0;

            while (dr.Read())
            {
                empid[i] = Convert.ToInt32(dr[0].ToString());
                ename[i] = dr[1].ToString();
                i++;


            }
            for(int j=0;j<i;j++)
                ListBox1.Items.Add(empid[j]+"-"+ename[j]);

            ListBox1.Items.Add("create new");
        }



    }


    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Table1.Visible =false;
        Table2.Visible = false;
        string comand = ListBox1.SelectedValue.ToString();
        if (comand == "create new")
        {
            Table1.Visible = true;
        }
        else
        {
            Table2.Visible = true;
            string[] emp = comand.Split('-');
            getdetails(Convert.ToInt32(emp[0]));


        }
    }
    static ListBox lb = new ListBox();           
    void getdetails(int empid)
    {

            managerid = empid;

                con.Open();
            com = new SqlCommand("select EmpId,EmpName from OrgChart where ManagerID=" + managerid + "", con);
            dr = com.ExecuteReader();

            lb.ID = "lb1";
            lb.AutoPostBack = true;


            while (dr.Read())
            {
                lb.Items.Add(dr[0].ToString() + "-" + dr[1].ToString());

            }
            lb.Items.Add("create new");

            lb.SelectedIndexChanged += new EventHandler(this.lb_SelectedIndexChanged);

            this.Controls.Add(lb);



    }

    void lb_SelectedIndexChanged(object sender, EventArgs e)
    {
        Table1.Visible = false;
        Table2.Visible = false;
        getdetails(managerid);
        string comand = ((ListBox)sender).SelectedValue.ToString();
        if (comand == "create new")
        {
            Table1.Visible = true;
        }
        else
        {
            Table2.Visible = true;

            string[] emp = comand.Split('-');
            getdetails(Convert.ToInt32(emp[0]));


        }
Posted

Hi,

Check this link for your problem.

click here to see suggestions.

And my opinion is If you are using Jquery then create listbox in jquery .

then using jquery post that data to server side whenever you like .


I hope you know that listbox is nothing but select tag .

this type of situations are dealing in jquery is easy than creating controls in asp.net

I hope you understand what i said.

Sure you can find solution in above link.

All the Best.
 
Share this answer
 
You should not check IsPostBack if control dynamically created.
 
Share this answer
 
Comments
navya12 29-Mar-12 2:31am    
how it is possible? if i chesked ispostback also no change is there in output

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