ASP.NET - dynamically setting panel width - Asked By SVK N on 22-Nov-11 08:11 AM

i have  panel1 in which i have a label in which  error msgs will be displayed
the error msgs is dynamic it can be one or multiple depending on data
currently i have fixed height
but if the msg is small panel become too big for it
or sometimes the msgs flow past the panel
how do i dynamicaly set the panel height
dipa ahuja replied to SVK N on 22-Nov-11 08:17 AM
You can set it this way::

  protected void Button1_Click(object sender, EventArgs e)
    Label1.Text = "hello worldhello worldhello worldhello worldhello worldhello world";
    Panel1.Width = Label1.Width;

Riley K replied to SVK N on 22-Nov-11 08:27 AM

Try setting the Width to Auto

<asp:Panel ID="panAddTools" runat="server" style="width:auto;" >

Try and let me know


Reena Jain replied to SVK N on 22-Nov-11 08:29 AM

I will suggest you that rather then increase the width of panel you can put scroll bar=auto so if text will increase then there would be scroll bar in panel itself

<asp:Panel ID="pnlDynamic" runat="server" BackColor="Fuchsia"
     Height="150px" Width="80%" HorizontalAlign="Center"
     Font-Names="Courier" ScrollBars="Auto">

try this and let me know


Devil Scorpio replied to SVK N on 23-Nov-11 08:18 AM

Find the complete code to set, change Panel width programmatically

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
        Panel1.Width = 200;
    protected void Button2_Click(object sender, System.EventArgs e)
        Panel1.Width = 300;

<html xmlns="">
<head id="Head1" runat="server">
    <title>How to set, change Panel width programmatically</title>
    <form id="form1" runat="server">
        <h2 style="color:Navy; font-style:italic;">Panel Example: Change Width</h2>
             <br />
             Change Width
        <br /><br />
             Text="Panel Width 200"
             Text="Panel Width 300"

You can save this code as PanelWidth.aspx

Output Display