C# += new Eventhandler not processing during postback

I have the code below creating dynamic linkbutton controls as well as a few panel controls depending on the menu items the user has selected. This is a 4 level menu, and I have the first 2 menu items working properly. The 3rd sub menu populates, but it doesn't hit the (lb3_click) that I have set for the 'Click' event. I create the ones that work in the OnInit part of the page, while the last 2 I create in the linkbutton click events, so I don't know if that is my issue or not. Take a look at the code, any help would be appreciated.

My code in question is created in the lb2_Click event.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
 
 
public partial class Product : DefaultPage
{
 
    //Added by hand; will create instance in OnInit.
    //create for each loop instead of hand coding each box.
    //this will eliminate the need to add more in the future when new items are added
    //append hl/tbtn names to temp table for use in OnInit
    class record
    {
        //assuming that every record to be dynamically represented in the UI is represented by an instance of the Record class
        public string MainCategory;
        public string SubCategory;
        public string SSCategory;
        public string SSSCategory;
 
    }
 
    protected System.Web.UI.WebControls.HyperLink hl;
    protected System.Web.UI.WebControls.LinkButton lb;
    protected System.Web.UI.WebControls.Panel ap;
    protected System.Web.UI.WebControls.LinkButton lb2;
    protected System.Web.UI.WebControls.LinkButton lb3;
    protected System.Web.UI.WebControls.LinkButton lb4;
    //protected System.Web.UI.WebControls.HyperLink hl6;
 
    private ProjDB _da = new ProjDB();
 
    String rProdCat;
    String rProdSubCat;
    String rProdSeries;
    String rProduct;
 
    String imgDir = "products/Image/";
 
    protected override void OnInit(EventArgs e)
    {
        //create dynamic controls here
        //use "using System.Web.UI.WebContrls;"
        //put in a for each loop, pulling hl/tbtn name from a temp table made above
 
        //Get Product Menu
        SqlDataAdapter da = new SqlDataAdapter("usp_GetProdCats",
            ConfigurationManager.ConnectionStrings["db"].ToString());
        da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        DataTable dt = new DataTable();
        da.Fill(dt);
 
        //Get Product SubMenu
        SqlDataAdapter da1 = new SqlDataAdapter("usp_GetProdSCats",
            ConfigurationManager.ConnectionStrings["db"].ToString());
        da1.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
 
        //Get Product SSMenu
        SqlDataAdapter da2 = new SqlDataAdapter("usp_GetProdSeries",
            ConfigurationManager.ConnectionStrings["db"].ToString());
        da2.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        DataTable dt2 = new DataTable();
        da2.Fill(dt2);
 
        //Get Product SSSMenu
        SqlDataAdapter da3 = new SqlDataAdapter("usp_GetProduct",
            ConfigurationManager.ConnectionStrings["db"].ToString());
        da3.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        DataTable dt3 = new DataTable();
        da3.Fill(dt3);
 
        String dProdCatID = "";
        String dProdSCatID = "";
        String dProdSSCatID = "";
        String dProdSSSCatID = "";
        String styleDef = "";
        String divID = "";
        String sAnchor = "";
        // if (!Page.IsPostBack)
        //  {
 
 
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //Category
            ProdSelLeft.Controls.Add(GetLiteral("<div>"));
            dProdCatID = dt.Rows[i]["ProdCatID"].ToString();
            lb = new LinkButton();
            lb.ID = "lb" + dProdCatID.ToString();
            lb.CssClass = "dropmenudiv";
            lb.Text = dt.Rows[i]["ProdCatName"].ToString();
            lb.EnableViewState = true;
            lb.CommandArgument = dProdCatID.ToString();
            lb.Click += new EventHandler(lb_Click);
            styleDef = "none";
            //Put If statement in here to place the menu data in the correct panel
            if (dProdCatID == "1")
            {
                panelMenu1.Controls.Add(lb);
                panelSMenu1.Visible = false;
                for (int i2 = 0; i2 < dt1.Rows.Count; i2++)
                {
                    //SubCategory
                    if (dProdCatID == dt1.Rows[i2]["ProdCatID"].ToString())
                    {
                        panelSMenu1.Controls.Add(GetLiteral("<div>"));
                        dProdSCatID = dt1.Rows[i2]["ProdSubCatID"].ToString();
                        lb2 = new LinkButton();
                        lb2.ID = "lb2" + i2.ToString(); // +dt.Rows[i]["ProdCatName"].ToString();
                        lb2.Text = dt1.Rows[i2]["ProdSubCatName"].ToString();
                        lb2.EnableViewState = true;
                        lb2.CommandArgument = dProdSCatID.ToString();
                        lb2.Click += new EventHandler(lb2_Click);
                        ap = new Panel();
                        ap.ID = "ap" + dt1.Rows[i2]["ProdSubCatID"].ToString();
                        panelSMenu1.Controls.Add(GetLiteral("&nbsp;&nbsp;&nbsp;&nbsp;"));
                        panelSMenu1.Controls.Add(lb2);
                        panelSMenu1.Controls.Add(GetLiteral("<br/>"));
                        panelSMenu1.Controls.Add(ap);
                        panelSMenu1.Controls.Add(GetLiteral("</div>"));
                    }
                }
            }
 
 
            //Think about if postback to see if I can get the menu cat item number that was clicked
            //to then make the panelSMenu visible and add that select sub category menu
            //Also, may be easier, to load SMenu data on this function, then on the onclick make whatever
            //one(using a variable) is clicked make visible and so on going down the line
            //saving each variable in a viewstate box then load the breadcrumbs from that
 
            if (dProdCatID == "2")
            {
                panelMenu2.Controls.Add(lb);
                panelSMenu2.Visible = false;
                for (int i2 = 0; i2 < dt1.Rows.Count; i2++)
                {
                    //SubCategory
                    if (dProdCatID == dt1.Rows[i2]["ProdCatID"].ToString())
                    {
                        panelSMenu2.Controls.Add(GetLiteral("<div>"));
                        lb2 = new LinkButton();
                        lb2.ID = "lb2" + i2.ToString(); // +dt.Rows[i]["ProdCatName"].ToString();
                        lb2.Text = dt1.Rows[i2]["ProdSubCatName"].ToString();
                        lb2.EnableViewState = true;
                        lb2.Click += new EventHandler(lb2_Click);
                        ap = new Panel();
                        ap.ID = "ap" + dt1.Rows[i2]["ProdSubCatID"].ToString();
                        panelSMenu2.Controls.Add(GetLiteral("&nbsp;&nbsp;&nbsp;&nbsp;"));
                        panelSMenu2.Controls.Add(lb2);
                        panelSMenu2.Controls.Add(GetLiteral("<br/>"));
                        panelSMenu2.Controls.Add(ap);
                        panelSMenu2.Controls.Add(GetLiteral("</div>"));
                    }
 
                }
            }
                if (dProdCatID == "3")
                {
                    panelMenu3.Controls.Add(lb);
                    panelSMenu3.Visible = false;
                    for (int i3 = 0; i3 < dt1.Rows.Count; i3++)
                    {
                        //SubCategory
                        if (dProdCatID == dt1.Rows[i3]["ProdCatID"].ToString())
                        {
                            panelSMenu3.Controls.Add(GetLiteral("<div>"));
                            lb2 = new LinkButton();
                            lb2.ID = "lb3" + i3.ToString(); // +dt.Rows[i]["ProdCatName"].ToString();
                            lb2.Text = dt1.Rows[i3]["ProdSubCatName"].ToString();
                            lb2.EnableViewState = true;
                            lb2.Click += new EventHandler(lb2_Click);
                            ap = new Panel();
                            ap.ID = "ap" + dt1.Rows[i3]["ProdSubCatID"].ToString();
                            panelSMenu3.Controls.Add(GetLiteral("&nbsp;&nbsp;&nbsp;&nbsp;"));
                            panelSMenu3.Controls.Add(lb2);
                            panelSMenu3.Controls.Add(GetLiteral("<br/>"));
                            panelSMenu3.Controls.Add(ap);
                            panelSMenu3.Controls.Add(GetLiteral("</div>"));
                        }
 
                    }
                }
                if (dProdCatID == "4")
                {
                    panelMenu4.Controls.Add(lb);
                    panelSMenu4.Visible = false;
                    for (int i4 = 0; i4 < dt1.Rows.Count; i4++)
                    {
                        //SubCategory
                        if (dProdCatID == dt1.Rows[i4]["ProdCatID"].ToString())
                        {
                            panelSMenu4.Controls.Add(GetLiteral("<div>"));
                            lb2 = new LinkButton();
                            lb2.ID = "lb4" + i4.ToString(); // +dt.Rows[i]["ProdCatName"].ToString();
                            lb2.Text = dt1.Rows[i4]["ProdSubCatName"].ToString();
                            lb2.EnableViewState = true;
                            lb2.Click += new EventHandler(lb2_Click);
                            ap = new Panel();
                            ap.ID = "ap" + dt1.Rows[i4]["ProdSubCatID"].ToString();
                            panelSMenu4.Controls.Add(GetLiteral("&nbsp;&nbsp;&nbsp;&nbsp;"));
                            panelSMenu4.Controls.Add(lb2);
                            panelSMenu4.Controls.Add(GetLiteral("<br/>"));
                            panelSMenu4.Controls.Add(ap);
                            panelSMenu4.Controls.Add(GetLiteral("</div>"));
                        }
 
                    }
                }
                if (dProdCatID == "5")
                {
                    panelMenu5.Controls.Add(lb);
                    panelSMenu5.Visible = false;
                    for (int i5 = 0; i5 < dt1.Rows.Count; i5++)
                    {
                        //SubCategory
                        if (dProdCatID == dt1.Rows[i5]["ProdCatID"].ToString())
                        {
                            panelSMenu5.Controls.Add(GetLiteral("<div>"));
                            lb2 = new LinkButton();
                            lb2.ID = "lb5" + i5.ToString(); // +dt.Rows[i]["ProdCatName"].ToString();
                            lb2.Text = dt1.Rows[i5]["ProdSubCatName"].ToString();
                            lb2.EnableViewState = true;
                            lb2.Click += new EventHandler(lb2_Click);
                            ap = new Panel();
                            ap.ID = "ap" + dt1.Rows[i5]["ProdSubCatID"].ToString();
                            panelSMenu5.Controls.Add(GetLiteral("&nbsp;&nbsp;&nbsp;&nbsp;"));
                            panelSMenu5.Controls.Add(lb2);
                            panelSMenu5.Controls.Add(GetLiteral("<br/>"));
                            panelSMenu5.Controls.Add(ap);
                            panelSMenu5.Controls.Add(GetLiteral("</div>"));
                        }
 
                    }
                }
                if (dProdCatID == "6")
                {
                    panelMenu6.Controls.Add(lb);
                    panelSMenu6.Visible = false;
                    for (int i6 = 0; i6 < dt1.Rows.Count; i6++)
                    {
                        //SubCategory
                        if (dProdCatID == dt1.Rows[i6]["ProdCatID"].ToString())
                        {
                            panelSMenu6.Controls.Add(GetLiteral("<div>"));
                            lb2 = new LinkButton();
                            lb2.ID = "lb6" + i6.ToString(); // +dt.Rows[i]["ProdCatName"].ToString();
                            lb2.Text = dt1.Rows[i6]["ProdSubCatName"].ToString();
                            lb2.EnableViewState = true;
                            lb2.Click += new EventHandler(lb2_Click);
                            ap = new Panel();
                            ap.ID = "ap" + dt1.Rows[i6]["ProdSubCatID"].ToString();
                            panelSMenu6.Controls.Add(GetLiteral("&nbsp;&nbsp;&nbsp;&nbsp;"));
                            panelSMenu6.Controls.Add(lb2);
                            panelSMenu6.Controls.Add(GetLiteral("<br/>"));
                            panelSMenu6.Controls.Add(ap);
                            panelSMenu6.Controls.Add(GetLiteral("</div>"));
                        }
 
                    }
                }
                ProdSelLeft.Controls.Add(GetLiteral("<br/>"));
                ProdSelLeft.Controls.Add(GetLiteral("</div>"));
            }
        
        }
    //  }
 
    protected void lb_Click(object sender, EventArgs e)//, string dProdCatID)
    {
        //Get Product Menu
        SqlDataAdapter da = new SqlDataAdapter("usp_GetProdCats",
            ConfigurationManager.ConnectionStrings["db"].ToString());
        da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        DataTable dt = new DataTable();
        da.Fill(dt);
 
        /*//Get Product SubMenu
        SqlDataAdapter da1 = new SqlDataAdapter("usp_GetProdSCats",
            ConfigurationManager.ConnectionStrings["db"].ToString());
        da1.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
 
        String dProdCatID = "";*/
        LinkButton b = (LinkButton)sender;
        string value = b.CommandArgument;
        ViewState["MenuID"] = b.CommandArgument;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (value == dt.Rows[i]["ProdCatID"].ToString())
            {
                ViewState["MenuSelection"] = dt.Rows[i]["ProdCatName"].ToString();
            }
        }
 
        if (Page.IsPostBack == true)
        {
            if (value == "1")
            {
                panelSMenu1.Visible = true;
                breadCrumbs.Visible = true;
                lb = new LinkButton();
                lb.ID = "bc1";
                lb.Text = ViewState["MenuSelection"] + " >";
                breadCrumbs.Controls.Add(lb);
            }
            if (value == "2")
            {
                panelSMenu2.Visible = true;
                breadCrumbs.Visible = true;
                lb = new LinkButton();
                lb.ID = "bc1";
                lb.Text = ViewState["MenuSelection"] + " >";
                breadCrumbs.Controls.Add(lb);
            }
            if (value == "3")
            {
                panelSMenu3.Visible = true;
                breadCrumbs.Visible = true;
                lb = new LinkButton();
                lb.ID = "bc1";
                lb.Text = ViewState["MenuSelection"] + " >";
                breadCrumbs.Controls.Add(lb);
            }
            if (value == "4")
            {
                panelSMenu4.Visible = true;
                breadCrumbs.Visible = true;
                lb = new LinkButton();
                lb.ID = "bc1";
                lb.Text = ViewState["MenuSelection"] + " >";
                breadCrumbs.Controls.Add(lb);
            }
            if (value == "5")
            {
                panelSMenu5.Visible = true;
                breadCrumbs.Visible = true;
                lb = new LinkButton();
                lb.ID = "bc1";
                lb.Text = ViewState["MenuSelection"] + " >";
                breadCrumbs.Controls.Add(lb);
            }
            if (value == "6")
            {
                panelSMenu6.Visible = true;
                breadCrumbs.Visible = true;
                lb = new LinkButton();
                lb.ID = "bc1";
                lb.Text = ViewState["MenuSelection"] + " >";
                breadCrumbs.Controls.Add(lb);
            }
        }
    }
 
    protected void lb2_Click(object sender, EventArgs e)
    {
        //Get Product SSMenu
        SqlDataAdapter da2 = new SqlDataAdapter("usp_GetProdSeries",
            ConfigurationManager.ConnectionStrings["db"].ToString());
        da2.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        DataTable dt2 = new DataTable();
        da2.Fill(dt2);
        
        System.Web.UI.WebControls.Panel p1;
        //p1 = ctype(panelSMenu1.FindControl("ctl00_body_ap11"));
 
        //Set panelSMenu + CommandArgument number
        //System.Web.UI.WebControls.Panel p2;
        //p2 = (System.Web.UI.WebControls.Panel)panelSMenu + ViewState["MenuSelection"];
        String p2 = "panelSMenu" + ViewState["MenuID"];
        Panel p3 = new Panel();
        p3 = (System.Web.UI.WebControls.Panel)ProdSelLeft.FindControl(p2);
 
        LinkButton b = (LinkButton)sender;
        string value = b.CommandArgument;
        //Do another if for loop, use the value as the argument on which SSMenu items to load
        //may need to think about figuring out how to add the new elements directly under the selected item
        //dynamically make an asp:panel in the sub menu run, hidden for each selection, then add those here via using the value and ap argument?
 
        //Do a find control to find the bc1 to keep on the page. make bc2 for the second bread crumb trail
 
        String panelID2 = "ap" + value.ToString();
        p1 = (System.Web.UI.WebControls.Panel)p3.FindControl(panelID2);   ///<--- Have to look and see if this can be changed to panelSMenu + ProdCatID
        for (int i7 = 0; i7 < dt2.Rows.Count ; i7++)
        {
            if(value == dt2.Rows[i7]["ProdSubCatID"].ToString())
            {
            lb3 = new LinkButton();
            lb3.ID = "lb3" + dt2.Rows[i7]["ProdSeriesID"].ToString();
            lb3.Text = dt2.Rows[i7]["ProdSeriesName"].ToString();
            lb3.CommandArgument = dt2.Rows[i7]["ProdSeriesID"].ToString();
            lb3.Click += new EventHandler(lb3_Click); 
            p1.Controls.Add(GetLiteral("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
            p1.Controls.Add(lb3);
            p1.Controls.Add(GetLiteral("<br/>"));
            p1.Visible = true;
            }
        }
    }
 
    protected void lb3_Click(object sender, EventArgs e)
    {
        //Get Product SSSMenu
        SqlDataAdapter da3 = new SqlDataAdapter("usp_GetProduct",
            ConfigurationManager.ConnectionStrings["db"].ToString());
        da3.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
        DataTable dt3 = new DataTable();
        da3.Fill(dt3);
 
        System.Web.UI.WebControls.Panel p2;
        LinkButton c = (LinkButton)sender;
        string value2 = c.CommandArgument;
        String panelID3 = "ap" + value2.ToString();
        
        //Do a find control to find the bc1 to keep on the page. make bc2 for the second bread crumb trail and bc3 for breadcrumb trail
        String p4 = "panelSMenu" + ViewState["MenuID"];
        Panel p3 = new Panel();
        p3 = (System.Web.UI.WebControls.Panel)ProdSelLeft.FindControl(p4);
        System.Web.UI.WebControls.Panel p1;
        String panelID2 = "ap" + value2.ToString();
        p1 = (System.Web.UI.WebControls.Panel)p3.FindControl(panelID2);
 
        for (int i = 0; i < dt3.Rows.Count; i++)
        {
            if (value2 == dt3.Rows[i]["ProdSeriesID"].ToString())
            {
                lb4 = new LinkButton();
                lb4.ID = dt3.Rows[i]["ProdID"].ToString();
                lb4.Text = dt3.Rows[i]["ProdName"].ToString();
                lb4.CommandArgument = dt3.Rows[i]["ProdID"].ToString();
                lb4.Click += new EventHandler(lb4_Click);
 
            }
        }
    }
 
    protected void lb4_Click(object sender, EventArgs e)
    {
        //add product load information
        //make sure to keep bread crumb trail
 
 
 
    }

Open in new window

GLIanimalAsked:
Who is Participating?
 
GLIanimalAuthor Commented:
Found the answer myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.