Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NETWebForms
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:23
navya12129


2 solutions

You should not check IsPostBack if control dynamically created.
  Permalink  
Comments
navya12 - 29 Mar '12 - 2:31
how it is possible? if i chesked ispostback also no change is there in output
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 365
1 OriginalGriff 315
2 Slacker007 240
3 Dave Kreskowiak 212
4 Aarti Meswania 210
0 Sergey Alexandrovich Kryukov 8,893
1 OriginalGriff 7,134
2 CPallini 3,678
3 Rohan Leuva 3,036
4 Maciej Los 2,428


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 29 Mar 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid