Export (0) Print
Expand All

Panel.HorizontalAlign Property

Gets or sets the horizontal alignment of the contents within the panel.

[Visual Basic]
Public Overridable Property HorizontalAlign As HorizontalAlign
[C#]
public virtual HorizontalAlign HorizontalAlign {get; set;}
[C++]
public: __property virtual HorizontalAlign get_HorizontalAlign();
public: __property virtual void set_HorizontalAlign(HorizontalAlign);
[JScript]
public function get HorizontalAlign() : HorizontalAlign;
public function set HorizontalAlign(HorizontalAlign);

Property Value

One of the HorizontalAlign values. The default is NotSet.

Exceptions

Exception Type Condition
ArgumentException The horizontal alignment is not one of the HorizontalAlign values.

Remarks

Use this property to specify the alignment of the elements in the Panel.

Example

[Visual Basic, C#] The following example illustrates how to right-align or left-align the controls within the Panel.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
 <head>
    <script language="VB" runat="server">
 
    Sub Page_Load(sender As Object, e As EventArgs)
       Dim l As New Label()
       l.Text = "This panel contains a label control."
       Panel1.Controls.Add(l)
    End Sub
     
    Sub Button1_Click(sender As Object, e As EventArgs)
       If Panel1.HorizontalAlign = HorizontalAlign.Left Then
          Panel1.HorizontalAlign = HorizontalAlign.Right
          Button1.Text = "Left justify text within Panel"
       Else
          Panel1.HorizontalAlign = HorizontalAlign.Left
          Button1.Text = "Right justify text within Panel"
       End If
    End Sub
    
    </script>
 
 </head>
 <body>
    <h3>Panel Example</h3>
    <form runat=server>
 
       <asp:Panel id="Panel1" Height=200 Width=100 BackColor="Gainsboro"
            Wrap="True" HorizontalAlign="Right" runat="server"/>
 
       <p>
       <asp:Button id="Button1" OnClick="Button1_Click"
            Text="Left justify text within panel" runat="server"/>
 
    </form>
 </body>
 </html>
    

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
    <script language="C#" runat="server">
 
    void Page_Load(Object sender, EventArgs e) {
       Label l = new Label();
       l.Text = "This panel contains a label control.";
       Panel1.Controls.Add(l);
    }
     
    void Button1_Click(Object sender, EventArgs e) {
       if (Panel1.HorizontalAlign == HorizontalAlign.Left) {
          Panel1.HorizontalAlign = HorizontalAlign.Right;
          Button1.Text = "Left justify text within Panel";
       } 
       else {
          Panel1.HorizontalAlign = HorizontalAlign.Left;
          Button1.Text = "Right justify text within Panel";
       }
    }
    </script>
 
 </head>
 <body>
    <h3>Panel Example</h3>
    <form runat=server>
 
       <asp:Panel id="Panel1" Height=200 Width=100 BackColor="Gainsboro"
            Wrap="True" HorizontalAlign="Right" runat="server"/>
 
       <p>
       <asp:Button id="Button1" OnClick="Button1_Click"
            Text="Left justify text within panel" runat="server"/>
 
    </form>
 </body>
 </html>
    

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Panel Class | Panel Members | System.Web.UI.WebControls Namespace | HorizontalAlign

Show:
© 2014 Microsoft