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

Regards

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

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">
 
</Panel>

try this and let me know

 

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

Find the complete code to set, change Panel width programmatically

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to set, change Panel width programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy; font-style:italic;">Panel Example: Change Width</h2>
        <asp:Panel 
             ID="Panel1"
             runat="server"
             Height="100"
             Width="100"
             HorizontalAlign="Center"
             ForeColor="SaddleBrown"
             BackColor="AntiqueWhite"
             BorderColor="IndianRed"
             BorderWidth="2"
             Font-Size="X-Large"
             Font-Bold="true"
             >
             <br />
             Change Width
        </asp:Panel>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             ForeColor="IndianRed"
             Text="Panel Width 200"
             Height="45"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
        <asp:Button 
             ID="Button2" 
             runat="server"
             ForeColor="IndianRed"
             Text="Panel Width 300"
             Height="45"
             OnClick="Button2_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>

You can save this code as PanelWidth.aspx

Output Display

http://4.bp.blogspot.com/_P3ZJmNYimyo/SfH0ncEnqBI/AAAAAAAAEIQ/6LXKB6xEDmo/s1600-h/PanelWidth.gif 

http://2.bp.blogspot.com/_P3ZJmNYimyo/SfH0wUptcyI/AAAAAAAAEIY/jtGgScd4V0o/s1600-h/PanelWidth1.gif 

http://3.bp.blogspot.com/_P3ZJmNYimyo/SfH051X8xNI/AAAAAAAAEIg/pbuyxmo6wAE/s1600-h/PanelWidth2.gif