2

A seemingly simple question, but I haven't found the answer online. I have developed a web part with an adjustable height/width. After adding the web part to my web part page, I open the configuration panel. The properties of the web part under Appearance: "Should the Web Part have a fixed width?" defaults to "No. Adjust width to fit zone." The web part can then be set to a custom width by selecting "Yes" and setting the width.

All good so far. The web parts resize quite well unless you make them exceedingly skinny or wide. I would like to set my web part a minimum width of 250px, and a maximum of 1000px. How can I do this?

These parts are going to be added to hundreds of pages. Thus to minimise the amount of configuration necessary, I would also like the web part to default to having a fixed width of 400px, which is the size they look the best for most purposes. However, I'd still like the user to be able to change the configuration to "No. Adjust height to fit zone." or customise the width if it is too narrow for their purpose.

Setting the size of the panel to 400px works, but then the part cannot be customised.

I have tried adding the following property to the .webpart file:

    <property name="Width" type="string">400px</property>

I have also tried overriding the width attribute and setting a _width field to a default of 400px.

    public string _width;

    [PersonalizableAttribute]
    [WebPartStorageAttribute(Storage = Storage.Personal)]
    public string Width
    {
        get
        { 
            if (_width == null || _width == string.Empty) { _width = "400px"; }
            return _width;
        }
        set { _width = value; }
    }

I've also tried it like this:

    public System.Web.UI.WebControls.Unit _width;

    [PersonalizableAttribute]
    [WebPartStorageAttribute(Storage = Storage.Personal)]
    public override System.Web.UI.WebControls.Unit Width
    {
        get
        { 
            if (_width == null) { _width = new System.Web.UI.WebControls.Unit("400px"); }
            return _width;
        }
        set { _width = value; }
    }

but none of these appear to have any effect whatsoever.

1 Answer 1

2

So the problem is that the Width and Height properties do not expect strings, but Unit as you have used in your code example.

For XML:

<property name="Width" type="unit">600px</property>
<property name="Height" type="unit">600px</property>

as you see, the only real difference is the type, that is unit and not string.

Overriding the propery in C# seems overkill. What you could do is

this.Height = System.Web.UI.WebControls.Unit.Pixel(400); 
2
  • Simplicity itself! Thank you. What about setting a minimum/maximum size? Is there any way to add custom validation to the configuration?
    – 08Dc91wk
    Aug 8, 2013 at 9:56
  • No problem. Min-max is more tricky, basically because that is not a property of HTML elements at all, but rather on CSS. What you could do, if you for example have a finite number of scenarios, is to have some if-statements in your webpart that sets the this.Height and this.Weight differently depending on some variables :) Aug 8, 2013 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.