Click here to Skip to main content
15,895,841 members
Please Sign up or sign in to vote.
1.06/5 (4 votes)
See more:
I am unable to found Page controls (Check boxes,Radio button,Images).
Please help me.my code is below.


HTML
ASP.NET
<div id="divLeftpane"   runat="server" class="product-left-menu"></div>



C#
private void PopulateLeftSearchPane()
    {

        SearchDataContext objGetAspects = new SearchDataContext(CommonFunctions.getConnectionString());
        var Aspects = from c in objGetAspects.RV_GetAspects()
                      select new
                      {
                          c.AspectName,
                          c.AspectID,
                          c.AspectContext,
                          c.AspectDescription,
                          c.AspectType

                      };
        if (Aspects != null)
        {

            Button button = new Button();

            foreach (var cn in Aspects)
            {

                //open div group menu
                HtmlGenericControl divgroupmenu = new System.Web.UI.HtmlControls.HtmlGenericControl("divgroupmenu");
                divgroupmenu.Attributes.Add("class", "group-menu");

                //open div accordion heading
                HtmlGenericControl divaccordionheading = new System.Web.UI.HtmlControls.HtmlGenericControl("divaccordionheading");
                divaccordionheading.Attributes.Add("class", "accordion-heading");

                //Add Hyperlink for header
                HtmlAnchor hlheader = new HtmlAnchor();
                hlheader.HRef = "#";
                hlheader.InnerText = cn.AspectName;

                divaccordionheading.Controls.Add(hlheader);

                //open div accordion inner
                HtmlGenericControl divaccordioninner = new System.Web.UI.HtmlControls.HtmlGenericControl("divaccordioninner");
                divaccordioninner.Attributes.Add("class", "accordion-inner");

               ShowData(cn.AspectID, cn.AspectName,cn.AspectDescription, cn.AspectContext, cn.AspectType, divgroupmenu, divaccordionheading, divaccordioninner);


            }

        }

    }
    public void ShowData(Int64 AspectID, String strAspectName,String strAspectDescription, String strAspectContext, String strAspectType, HtmlGenericControl divgroupmenu, HtmlGenericControl divaccordionheading, HtmlGenericControl divaccordioninner)
    {

        Literal lculopen = new Literal();
        lculopen.Text = String.Empty;
        if (strAspectContext == "CHECK")
        {
            lculopen.Text = "<ul class="stone-type leftdrop">";
        }
        else if (strAspectContext == "OPTION")
        {
            lculopen.Text = "<ul class="ring-style leftdrop">";
        }
        else if (strAspectContext == "IMAGEMANY"  && strAspectDescription.Contains("SEQURE"))
        {
            lculopen.Text = "<ul class="sequremultiple leftdrop">";
        }
        else if (strAspectContext == "IMAGESINGLE" && strAspectDescription.Contains("SEQURE"))
        {
            lculopen.Text = "<ul class="sequresingle leftdrop">";
        }
        else if (strAspectContext == "IMAGEMANY"  && strAspectDescription.Contains("CIRCLE"))
        {
            lculopen.Text = "<ul class="circlemultiple leftdrop">";
        }
        else if (strAspectContext == "IMAGESINGLE" && strAspectDescription.Contains("CIRCLE"))
        {
            lculopen.Text = "<ul class="circlesingle leftdrop">";
        }
        else
        {
            lculopen.Text = "<ul class="stone-type leftdrop">";
        }

        divaccordioninner.Controls.Add(lculopen);

        SearchDataContext objGetAspects = new SearchDataContext(CommonFunctions.getConnectionString());
        var Aspects = from c in objGetAspects.RV_GetTraitValue(AspectID)
                      select new
                      {
                          c.AspectID,
                          c.TraitID,
                          c.ValueName
                      };


        foreach (var cn in Aspects)
        {

            if (strAspectContext == "CHECK")
            {
                Literal lcliopen = new Literal();
                lcliopen.Text = "<li>";
                divaccordioninner.Controls.Add(lcliopen);

                UpdatePanel myupdpnl = new UpdatePanel();
                myupdpnl.ID = "upnl" + cn.AspectID + "_" + cn.TraitID;

                CheckBox chk = new CheckBox();
                chk.ID = "chktrait" + cn.AspectID + "_" + cn.TraitID;
                //string uniqueID = System.Guid.NewGuid().ToString().Substring(0, 5);
                chk.Text = cn.ValueName;
                chk.Attributes.Add("AspectID", cn.AspectID.ToString());
                chk.Attributes.Add("TraitID", cn.TraitID.ToString());
                chk.AutoPostBack = false;
                //CheckBox chk = (CheckBox)ctrl;
              //  chk.CheckedChanged += new EventHandler(CheckBox_CheckChanged);
               // chk.CheckedChanged += new EventHandler(this.CheckBox_Click);
                chk.Attributes.Add("onchange", "Javascript:return CallSearchResults('"+ chk.ID  + "','"  + cn.AspectID +  "','"  + cn.TraitID + "','"  + cn.ValueName + "'"    + ");");
                myupdpnl.ContentTemplateContainer.Controls.Add(chk);
                //myupdpnl.ContentTemplateContainer.Controls.Add(chk);

                //divaccordioninner.Controls.Add(chk);
                divaccordioninner.Controls.Add(myupdpnl);


                Literal lcliclose = new Literal();
                lcliclose.Text = "</li>";
                divaccordioninner.Controls.Add(lcliclose);

            }
            else if (strAspectContext == "OPTION")
            {
                Literal lcliopen = new Literal();
                lcliopen.Text = "<li>";
                divaccordioninner.Controls.Add(lcliopen);

                RadioButton rdbtn = new RadioButton();
                rdbtn.ID = "rdbtntrait" + cn.AspectID + "_" + cn.TraitID;
                rdbtn.Text = cn.ValueName;
                rdbtn.AutoPostBack = false;
                rdbtn.GroupName = "rdbtntrait" + cn.AspectID;
                divaccordioninner.Controls.Add(rdbtn);

                Literal lcliclose = new Literal();
                lcliclose.Text = "</li>";
                divaccordioninner.Controls.Add(lcliclose);

            }
            else if (strAspectContext == "IMAGEMANY")
            {

                Literal lcliopen = new Literal();
                lcliopen.Text = "<li>";
                divaccordioninner.Controls.Add(lcliopen);

                //Add Hyperlink 
                HtmlAnchor htmlanchor = new HtmlAnchor();
                //htmlanchor.HRef = "#";

                Image img = new Image();
                img.ID = "imgtrait" + cn.AspectID + "_" + cn.TraitID;
                img.AlternateText = cn.ValueName;
                htmlanchor.Controls.Add(img);

                CommonFunctions objCommonFunctions = new CommonFunctions();
                String StrAspectIDTraitID = cn.AspectID + "-" + cn.TraitID;
                String imgtraitcons = CommonFunctions.searchImageFromBlob(StrAspectIDTraitID, objCommonFunctions.GetSystemVariables(CommonFunctions.SystemVariables.DataConnectionString), objCommonFunctions.GetSystemVariables(CommonFunctions.SystemVariables.traitconContainer)); //searchImageFromBlob(cn.ContentID.ToString());
                img.ImageUrl = imgtraitcons;
                divaccordioninner.Controls.Add(htmlanchor);
             
                Literal lcliclose = new Literal();
                lcliclose.Text = "</li>";
                divaccordioninner.Controls.Add(lcliclose);


            }
            else if (strAspectContext == "IMAGESINGLE")
            {

                Literal lcliopen = new Literal();
                lcliopen.Text = "<li>";
                divaccordioninner.Controls.Add(lcliopen);

                //Add Hyperlink 
                HtmlAnchor htmlanchor = new HtmlAnchor();
                //htmlanchor.HRef = "#";

                Image img = new Image();
                img.ID = "imgtrait" + cn.AspectID + "_" + cn.TraitID;
                img.AlternateText = cn.ValueName;
                htmlanchor.Controls.Add(img);

                CommonFunctions objCommonFunctions = new CommonFunctions();
                String StrAspectIDTraitID = cn.AspectID + "-" + cn.TraitID;
                String imgtraitcons = CommonFunctions.searchImageFromBlob(StrAspectIDTraitID, objCommonFunctions.GetSystemVariables(CommonFunctions.SystemVariables.DataConnectionString), objCommonFunctions.GetSystemVariables(CommonFunctions.SystemVariables.traitconContainer)); //searchImageFromBlob(cn.ContentID.ToString());
                img.ImageUrl = imgtraitcons;
                divaccordioninner.Controls.Add(htmlanchor);
              
                Literal lcliclose = new Literal();
                lcliclose.Text = "</li>";
                divaccordioninner.Controls.Add(lcliclose);


            }
            else
            {

            }

        }

        Literal lculclose = new Literal();
        lculclose.Text = "</ul>";
        divaccordioninner.Controls.Add(lculclose);
        //close div divaccordionheading
        divaccordionheading.Controls.Add(divaccordioninner);
        //close div divgroupmenu
        divgroupmenu.Controls.Add(divaccordionheading);
        //close div divLeftpane
        divLeftpane.Controls.Add(divgroupmenu);
    }

    private void ProcessControls(Control ctrlContainer)
    {
        foreach (Control ctrl in ctrlContainer.Controls)
        {
            if (ctrl.GetType() == typeof(TextBox))
            {
                // Do whatever to the TextBox
            }

            else if (ctrl.GetType() == typeof(CheckBox))
            {
                // Do whatever to the TextBox
                Response.Write(ctrl.UniqueID);

               
               
            }
            else if (ctrl.GetType() == typeof(RadioButton))
            {
                // Do whatever to the TextBox
            }
            else if (ctrl.GetType() == typeof(Image))
            {
                // Do whatever to the TextBox
            }

            if (ctrl.HasControls())
                ProcessControls(ctrl);
        }
    }

protected void btnSearchResult_Click(object sender, EventArgs e)
   {

       ProcessControls(divLeftpane);
      // PopulateLeftSearchPane();
   }
Posted
Updated 21-Jul-14 2:04am
v2
Comments
ZurdoDev 21-Jul-14 7:58am    
1. This is way too much code for someone to try and help you.
2. I assume this is ASP.Net? If so, dynamic controls should be created in the OnInit event of the page.
SRS(The Coder) 21-Jul-14 8:05am    
Too much of code to understand and give solution.
Please write small summary of all.
GregWyatt 22-Jul-14 16:51pm    
I would like to second RyanDev's statement that any dynamically added controls need to be done in the OnInit event. Below is a link to an article that discusses the asp.net life cycle, viewstate, and has a special section that explains some of the tricks with dynamically adding controls(how they maintain state across postbacks). http://msdn.microsoft.com/en-us/library/ms972976.aspx Its a long read, but if you are going to do dynamic controls it is worth it to understand what is happening.
member33 22-Jul-14 17:03pm    
I think you are loosing controls on post backs .. Dymanic controls cannot retain state after post back .. you will have to save them in a view state and regenerate on every post back

1 solution

 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900