locked
How to arrange asp:Panel child controls in one line RRS feed

  • Question

  • Hello,

    I have one Panel declared in aspx. I'll call it ParentPnl.

    Now, I want to dynamically add a number of panels to ParentPnl.

    I was sure that by default, the child panels will be displayed one after another, from left to right. What really happens is: the child panels displayed one under another, in one "column". Anything I tried so far didn't help.

    Also, I tried to add Labels instead of panels, and it was OK, they were displayed in one "row", and not in a "column".

    Here is the code that creates the child panels and adds them to Parent Panel:

    (I changed variable names, so if I forgot to change somewhere, it may seem like it will not compile)

     

    protected void createPanel(String strSIP, int nSNum)
        {
          int nSlt = 0;
          int nPrt = 0;
          String strType = String.Empty;
          String strResult = String.Empty;
          String strConnectedStation = String.Empty;
          List<List<Panel>> lSlts = new List<List<Panel>>();
    
          for (int i = 0; i < nSNum; i++)
          {
            List<Panel> lSlt = new List<Panel>();
            lSts.Add(lSlt);
          }
    
          foreach (DataRow drPrt in dtTable.Rows)
          {
            nSlt = (int)drPrt["nSlt"];
            nPrt = (int)drPrt["nPrt"];
            strType = drPrt["Type"].ToString();
            strResult = drPrt["result"].ToString();
            strConnectedStation = drPrt["strConnectedStation"].ToString();
    
            Panel pnlPrt = new Panel();
            Label lblPrt = new Label();
            Button btnPrt = new Button();
    
            lblPrt.Text = "&nbsp&nbsp" + nPrt.ToString();
            
            btnPrt.CssClass = "stylePrtIcon";
    
            btnPrt.ID = String.Format("{0},{1},{2}", nSlt.ToString(), nPrt.ToString(), strConnectedStation);
            btnPrt.Click += new EventHandler(showPrtDetails);
    
            switch (strResult)
            {
              case "SUCCESS":
                lblPrt.CssClass = "styleGreenRecIcon";
                break;
              case "FAILURE":
                lblPrt.CssClass = "styleRedRecIcon";
                break;
              case "UNKNOWN":
                lblPrt.CssClass = "styleGrayRecIcon";
                break;
              default:
                break;
            }
    
            switch (strType)
            {
              case "Unknown":
                btnPrt.CssClass = "styleQuestionIcon";
                break;
              case "Computer":
                btnPrt.CssClass = "styleComputerIcon";
                break;
              case "Printer":
                btnPrt.CssClass = "stylePrinterIcon";
                break;
              case "Network_Device":
                btnPrt.CssClass = "styleNetworkIcon";
                break;
              case "Server":
                btnPrt.CssClass = "styleServerIcon";
                break;
              case "Linux":
                btnPrt.CssClass = "styleLinuxIcon";
                break;
              case "Clock":
                btnPrt.CssClass = "styleClockIcon";
                break;
              default:
                break;
            }
    
            
            pnlPrt.Controls.Add(lblPrt);
            pnlPrt.Controls.Add(new LiteralControl("<br />"));
            pnlPrt.Controls.Add(btnPrt);
    
            lSlts[nSlt-1].Add(pnlPrt);
          }
    
          foreach(List<Panel> lPnlList in lSlts)
          {
            Panel pnlSlt = new Panel();
    
            foreach (Panel pnl in lPnlList)
            {
              pnlSlt.Controls.Add(pnl);
            }
    
            parentPanel.Controls.Add(pnlSlt);
            parentPanel.Controls.Add(new LiteralControl("<br />"));
          }
    
    //the following labels displayed fine:
          Panel pnl1 = new Panel();
          for (int i = 0; i < 6; i++)
          {
            Label lbl = new Label();
            lbl.Text = "Blablablablablabla ";
            pnl1.Controls.Add(lbl);
          }
          parentPanel.Controls.Add(pnl1);
        }
    

    Please Help me!

    Thursday, May 5, 2011 10:17 AM

Answers

  • Ok, I still don't know what is the problem, but I found a workaround.

    Instead of creating these small panels containing label and  button, and then adding them to the bigger panel, I added the labels and buttons direct to the bigger panel, and they were displayed in one line from left to right, as I wanted.

    Sunday, May 8, 2011 7:15 AM
  • Another solution is to create (dynamically) a table, and add a new cell for each panel to that table, and then to add that table to parent panel.
    • Marked as answer by TL_TL_TL Monday, June 20, 2011 12:23 PM
    Monday, June 20, 2011 12:23 PM

All replies

  • Ok, I still don't know what is the problem, but I found a workaround.

    Instead of creating these small panels containing label and  button, and then adding them to the bigger panel, I added the labels and buttons direct to the bigger panel, and they were displayed in one line from left to right, as I wanted.

    Sunday, May 8, 2011 7:15 AM
  • Glad to hear that you've got it resolved.
    Nice day!
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 9, 2011 7:01 AM
  • You are adding panels, but have no Page.Form to what you add that one.

    http://msdn.microsoft.com/en-us/library/system.web.ui.page.form.aspx

    It took me also 5 years before I discovered that this was the main cause of all these problems.

    :-)

     


    Success
    Cor
    Monday, May 9, 2011 7:55 AM
  • Thank you for you reply!

    Can you give me an example how to use Page.Form?

    Why do I need it?

     

    Nice day!

    TL.

    Monday, May 9, 2011 10:35 AM
  • Another solution is to create (dynamically) a table, and add a new cell for each panel to that table, and then to add that table to parent panel.
    • Marked as answer by TL_TL_TL Monday, June 20, 2011 12:23 PM
    Monday, June 20, 2011 12:23 PM