none
Dynamically Adding Layered Controls RRS feed

  • Question

  • I have a major problem. I'm working on a website and I have to generate controls dynamically that depend on each other. Let me explain as follows:

    I have 3 tables 1) Segment 2) Category 3)Product.

    A segment consists of various categories. Each categories contains various products.

    I am to create a navigation panel that first of all displays the segments.

    After clicking a particular segment, all categories under that segment are displayed on the panel below.

    And on clicking a particular category, the various products under that category is displayed in a panel below.

     

    The basic design for the navigation panel looks something like this:

    |---------------------------|

    |     Segment panel       |

    |---------------------------|

    |     Category panel       |

    |---------------------------|

    |     Product panel         |

    ----------------------------|

     

    I can dynamically create the segment panel and category panel. The problem is that when I click on any category, the controls in the Category panel disappear, and the product controls aren't generated. I used LinkButton controls to generate the various items in the segment, category and product panels.

     

    Here is the code snippet:

    public void generate_segment()
        {
            try
            {
                con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Solar\App_Data\Solar.mdf;Integrated Security=True;User Instance=True");
                cmd = new SqlCommand("select productSegmentCode, productSegmentName from productSegment", con);
                con.Open();
                sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    LinkButton lbtn = new LinkButton();
                    lbtn.ID = sdr[0].ToString();
                    lbtn.Text = sdr[1].ToString();
                    lbtn.Click += new EventHandler(lbtn_Click);
                    panel_seg.Controls.Add(lbtn);
                    panel_seg.Controls.Add(new LiteralControl("<br>"));
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                con.Close();
            }
        }

        public void lbtn_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            string segId = lb.ID.ToString();
            generate_category(segId);
        }
        public void generate_category(string id)
        {
            con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Solar\App_Data\Solar.mdf;Integrated Security=True;User Instance=True");
            cmd = new SqlCommand("select productCategoryCode, productCategoryName from productCategory where segmentCode='" + id + "'", con);
            con.Open();
            sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                LinkButton lbtn_cat = new LinkButton();
                lbtn_cat.ID = sdr[0].ToString();
                lbtn_cat.Text = sdr[1].ToString();
                lbtn_cat.Click+= new EventHandler(lbtn_cat_Click);
                panel_cat.Controls.Add(lbtn_cat);
                panel_cat.Controls.Add(new LiteralControl("<br>"));
            }
        }
        public void lbtn_cat_Click(object sender1, EventArgs e1)
        {
            LinkButton lb1 = (LinkButton)sender1;
            string catId = lb1.ID.ToString();
            generate_product(catId);
        }
        public void generate_product(string id)
        {
            con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Solar\App_Data\Solar.mdf;Integrated Security=True;User Instance=True");
            cmd = new SqlCommand("select productCode, productName from product where categoryCode='" + id + "'", con);
            LinkButton lbtn_prod = new LinkButton();
            lbtn_prod.ID = sdr[0].ToString();
            lbtn_prod.Text = sdr[1].ToString();
            lbtn_prod.Click += new EventHandler(lbtn_prod_Click);
            panel_prod.Controls.Add(lbtn_prod);
            panel_prod.Controls.Add(new LiteralControl("<br>"));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            generate_segment();
        }

    }

     

    Could someone kindly help me out with this problem. Its annoying me as ____ and I need to get around this problem as soon as possible.

    Thanks,

    Adrian.

    Monday, August 16, 2010 6:21 PM

Answers