[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# += new Eventhandler not processing during postback

Posted on 2009-02-10
1
Medium Priority
?
763 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:GLIanimal
1 Comment
 

Accepted Solution

by:
GLIanimal earned 0 total points
ID: 23604797
Found the answer myself.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question