Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Im getting this error when trying to add a custome property to a webpart, what have I done wrong?

    [WebBrowsable(true),
    Personalizable(true),
    WebDisplayName("Site Root"),
    WebDescription("blah blah"),
    Category("Custome Settings"),
    WebPartStorage(Storage.Shared)]
    public string SiteRoot
    {
        set { HttpContext.Current.Session["SiteRoot"] = value; }
    }

(Image of error is here, cant put it the question as new users cant use the image tag!?! wtf?)

http://www.freeimagehosting.net/uploads/d1bdd5989c.png

share|improve this question
add comment

2 Answers 2

A web part property is used to bind configuration settings to and from the web part settings database.

Your command:

set { HttpContext.Current.Session["SiteRoot"] = value; }

does not make sense to put in a property

It should be something like this

private string _siteroot;
[WebBrowsable(true),
    Personalizable(true),
    WebDisplayName("Site Root"),
    WebDescription("blah blah"),
    Category("Custome Settings"),
    WebPartStorage(Storage.Shared)]
public string SiteRoot {
    get { return _siteroot; }
    set { _siteroot = value; }
}

And then you can use the property like this

protected override void OnLoad(EventArgs e) {
    base.OnLoad(e);

    if(!IsPostBack) {
        HttpContext.Current.Session["SiteRoot"] = SiteRoot;
    }
}
share|improve this answer
    
Thanks thats fixed it, but how do I access the property in the code behind of the usercontrol for my Visual webpart. –  Dan Jan 13 '11 at 12:39
    
its public, it should be accessible by <%= SiteRoot %> –  djeeg Jan 13 '11 at 12:48
add comment

See this post. It shows creating a custom property, and accessing that property from the code behind of your user control.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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