Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET WebForms
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....
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 28-Mar-12 20:23pm
navya12132
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should not check IsPostBack if control dynamically created.
  Permalink  
Comments
navya12 at 29-Mar-12 2:31am
   
how it is possible? if i chesked ispostback also no change is there in output
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 CPallini 385
1 Sergey Alexandrovich Kryukov 262
2 OriginalGriff 200
3 George Jonsson 134
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,890
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 29 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100