Click here to Skip to main content
16,016,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a web page that loads image buttons and other controls, where these buttons leads to another image buttons and other controls that have event handlers that do the same for illustration:

1- When the page loads i need to view all parent folders as image buttons using database:
C#
protected void popAllParent()
    {
        Reporting repo = new Reporting();
        foreach (Reporting item in repo.getAllFolders())
        {
            Label labelFold = new Label();
            CheckBox chb = new CheckBox();
            RadioButton rad = new RadioButton();
            ImageButton imgBtn = new ImageButton();

                labelFold.Text = " " + item.FolderName;
                labelFold.Attributes.Add("pfolderID", item.FoldID);

                chb.Attributes.Add("pfolderID", item.FoldID);

                rad.Attributes.Add("pfolderID", item.FoldID);
                rad.GroupName = "foldDelete";

                imgBtn.ImageUrl = "~/Style/images/folder.png";
                imgBtn.ID = "imgPfold" + item.FoldID;
                imgBtn.Attributes.Add("pfolderID", item.FoldID);
                imgBtn.Width = 50;
                imgBtn.Height = 50;
                imgBtn.Click += new ImageClickEventHandler(ImageButton1_Click);

                Panel1.Controls.Add(labelFold);
                Panel1.Controls.Add(new LiteralControl("  "));
                Panel1.Controls.Add(chb);
                Panel1.Controls.Add(new LiteralControl(" "));
                Panel1.Controls.Add(rad);
                Panel1.Controls.Add(new LiteralControl("<br />"));
                Panel1.Controls.Add(imgBtn);
                Panel1.Controls.Add(new LiteralControl("<br />"));
            }

        }

2- for each img button there is an event handler leads to subfolders method:
C#
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtn = (ImageButton)sender;
        ViewState["Sample"] = ibtn.Attributes["pfolderID"].ToString();
        popAllSub(ibtn.Attributes["pfolderID"].ToString());
    }

    protected void popAllSub(string pfID)
    {
        Reporting repo = new Reporting();

        Panel1.Controls.Clear();

        foreach (Reporting item in repo.getAllSubFolders(pfID))
        {
            Label labelFold = new Label();
            CheckBox chb = new CheckBox();
            RadioButton rad = new RadioButton();
            ImageButton imgBtn = new ImageButton();

            labelFold.Text = " " + item.FolderName;
            labelFold.Attributes.Add("pfolderID", item.FoldID);

            chb.Attributes.Add("pfolderID", item.FoldID);


            rad.Attributes.Add("pfolderID", item.FoldID);
            rad.GroupName = "foldDelete";

            imgBtn.ImageUrl = "~/Style/images/folder.png";
            imgBtn.Attributes.Add("pfolderID", item.FoldID);
            imgBtn.ID = "imgSfold" + item.FoldID;
            imgBtn.Width = 35;
            imgBtn.Height = 35;
            imgBtn.Click += new ImageClickEventHandler(ImageButton1_Click);

            Panel2.Controls.Add(labelFold);
            Panel2.Controls.Add(new LiteralControl("  "));
            Panel2.Controls.Add(chb);
            Panel2.Controls.Add(new LiteralControl(" "));
            Panel2.Controls.Add(rad);
            Panel2.Controls.Add(new LiteralControl("<br />"));
            Panel2.Controls.Add(imgBtn);
            Panel2.Controls.Add(new LiteralControl("<br />"));
        }
}

3- Folders in db is like this:
fName        fID     fType     parentID
---------------------------------------
Quality       3      Child     1
Summary       1      Parent 
Finance       2      Child     1
Quality       4      Parent 
process       5      Child     4
processes     6      Child     4
Purchasing    7      Child     1
testFold      8      Child     7

As you see there are subfolder and inside it another subfolders. when the parent folders clicked, the event view the subfolders and the parent folders disappear, and when i try to click on the sub folders, every thing is disappeared and not reading the event handler. Please help me.
Posted
Updated 18-Mar-14 0:49am
v2
Comments
ZurdoDev 18-Mar-14 7:10am    
I can't tell from your code but you'll want to add dynamic controls in the page_Init event so that they get included in the ViewState. It happens before Page_Load.
Mohamed Ghandour 20-Mar-14 1:37am    
at the page_init it also make the same problem the Parent folders disappear and the subfolders click handler is not fired

1 solution

Thanks I got a better and easier way to solve this problem using this link
 
http://www.igregor.net/post/2007/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx
 
And you can use array of strings to pass attributes.
 
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