Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Anyone help me how can I set label generated in C# to be positioned at the center of the whole tab panel? (Please see below link)

This is the output I want to achieve:
http://i.stack.imgur.com/BE8WY.png[^]

This is my code:
C#
if(panel.Controls.Count<1)
                {
                    System.Web.UI.HtmlControls.HtmlGenericControl panelDIV = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                    panelDIV.ID = "panelDIV";
                    panelDIV.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
                    panelDIV.Style.Add(HtmlTextWriterStyle.Width, "100%");
                    panelDIV.Style.Add(HtmlTextWriterStyle.Position, "center");
                    panelDIV.Style.Add(HtmlTextWriterStyle.FontStyle, "Arial");


                    System.Web.UI.WebControls.Label newTabel = new System.Web.UI.WebControls.Label();
                    newTabel.Text +=  "no chart to display";
                    newTabel.Font.Bold = true;
                    newTabel.Font.Size=16;
                    newTabel.Style.Add("text-align", "center");
                    panelDIV.Controls.Add(newTabel);
                    panel.Controls.Add(panelDIV);
                }


Question: how to set label generated in C# to be positioned at the center of the whole tab panel?
Thanks.
Posted
Updated 28-Oct-15 6:06am
v6

You can do it in following ways:

Option1: Using Style.Add()
C#
newTabel.Style.Add("text-align", "center");

Option2: Using CSS class

Add following class in apsx file
CSS
.MyClass {
    text-align: center;
}

Add following code in code-behind file:
C#
newTabel.CssClass = "MyClass";
 
Share this answer
 
v2
Comments
Member 11999641 28-Oct-15 12:06pm    
hi Manas_Kumar, thanks for your reply. have tried your solution but still get the same output. I would like the label to appear at the center of the whole tab panel, please look at my updated code thanks!
Vertical centering is one of the hardest things to do in CSS. :)

Centering in CSS: A Complete Guide[^]

If you can use Flexbox[^], then it's relatively simple:
CSS
.parent {
    display: flex;
    justify-content: center;
    align-items: center;
}

If you can use 2D transforms[^], then this should work:
CSS
.parent {
    position: relative;
}
.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

If you're stuck with supporting IE8 or earlier, then there's no real solution, unless both the parent and child elements are a fixed height.
 
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