SharePoint - Webpart Property doesnt keep its value

Asked By Jacob Söderblom on 20-Apr-12 07:16 AM
I want to store a DateTime in a webpart property, but it wont keep its value.. Here is my code:

[WebBrowsable(true), Personalizable(PersonalizationScope.Shared), WebDisplayName("Today"), Category("Today")]
        public string Today { get; set; }


        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/SP.TipOfTheDay/TipOfTheDay/TipOfTheDayUserControl.ascx";


        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            if (control != null)
            {
                ((TipOfTheDayUserControl)control).WebPart = this;
            }
            Controls.Add(control);
        }

And then in usercontrol.ascx.cs

 public TipOfTheDay WebPart { get; set; }

 string today = DateTime.Now.ToShortDateString();
                        WebPart.Today = today;
Please help!



Jacob Söderblom replied to Somesh Yadav on 20-Apr-12 08:53 AM
Doesn't got any validates.. And now my Label are empty every time i reload the page :S

Edit:
Now it works with one of my property but not the other one.. Even if they are exactly the same..
[)ia6l0 iii replied to Jacob Söderblom on 22-Apr-12 11:01 PM
Hold a sec.

I assume "Today" is the property that we are talking about.  

a) Why is it a string instead of datetime?
b) How and where did you assign the value to this property. That does not seem to be done at the right place. 
c) In the usercontrol, why are you creating an instance of "TipOfTheDay"? Aren't they same? d) Is the "TipOfTheDayUserControl " and the "TipOfTheDay" same?


Jitendra Faye replied to Jacob Söderblom on 23-Apr-12 12:50 AM
I have seen your code you have defined one property-


 public TipOfTheDay WebPart { get; set; }

But while assigning you are not usiong this property .

change your code like this-

 string today = DateTime.Now.ToShortDateString();

WebPart. TipOfTheDay =today;

TRy this and let me know.

Jacob Söderblom replied to [)ia6l0 iii on 23-Apr-12 02:22 AM
Yes "Today" is a property that i want to put a value to.

A) It´s a string cuz I want to have ToShortDateString();, but that might work in DateTime as well?
B) Here is some code from the "TipOfTheDayUserControl.ascx.cs".. It´s not the greates code i know, but i´m still kinda novice to SharePoint and C#.. 
public partial class TipOfTheDayUserControl : UserControl
    {
        public TipOfTheDay WebPart { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb oWeb = SPContext.Current.Web;
            _lblTipOfTheDay.Text = this.WebPart.String;
            if (!Page.IsPostBack)
            {
                if (oWeb.Lists.TryGetList("TODList") == null)
                {
                    CreateList();
                }


                string curDate = DateTime.Now.ToShortDateString();
                if (curDate != this.WebPart.Today || _lblTipOfTheDay.Text == "")
                {
                    _lblTipOfTheDay.Text = GetTOD();
                }
               
            }
            string today = DateTime.Now.ToShortDateString();
            this.WebPart.Today = today;
            this.WebPart.CurrentText = _lblTipOfTheDay.Text;
        }
}
C) This is a visual webpart, and to create properties in a visual webpart you need to create the properties in webpart.cs file, in my case "TipOfTheDay.cs". And then make an instance of that file in your "WebpartUserControl.ascx.cs", in my case "TipOfTheDayUserControl.ascx.cs" to get access to them. That´s what i have learned..
D) They are in the same WebPart yes, but it´s not the same file.

Hope this did make it clearer! =)