Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Why isn't my property retaining it's value?

$
0
0

Simple set up with a provider/consumer. Below is the code for the consumer. In question is the OnPreRender method. In it I basically want to do a simple test of whether or not the provider has changed it's value. If it has then I need to reload my dropdown, if not then do nothing.

I set up a very simple property (LastContractID) to store the value, then when I come to OnPreRender I check to see if the current equals the Last. I tried with just a simple String member variable but it never would retain it's value. The only way I could get it to stick was to make it a property and mark it up with [Personalizable(PersonalizationScope.Shared)]

That makes it work fine for a user with the right permissions, but I need it to work even for a user with ReadOnly permissions. The ReadOnly user can't set the Personalizable property so it doesn't serve it's purpose.

How can I set up a simple member variable to keep track of the last value that will actually retain it's value?

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data.SqlClient;

namespace ConnectingWebParts.StringConsumer
{
    [ToolboxItemAttribute(false)]
    public class StringConsumer : WebPart, IPeriodID
    {
        private IContractID _myProvider;
        private String _periodID;
        private String _contractID;
        private Label myLabel;
        private DropDownList myDropDownList;
        private Button myButton;

        public string PeriodID
        {
            get
            {
                return _periodID;
            }
            set
            {
                _periodID = value;
            }
        }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(false)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string LastContractID
        {
            get
            {
                return _contractID;
            }
            set
            {
                _contractID = value;
            }
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            
            myDropDownList = new DropDownList();
            myDropDownList.Items.Add("--SELECT PERIOD--");
            Controls.Add(myDropDownList);
            myButton = new Button();
            myButton.Text = "Display Period ID";
            Controls.Add(myButton);
            myButton.Click += new EventHandler(myButton_Click);

        }

        protected override void OnPreRender(EventArgs e)
        {
            EnsureChildControls();
            if (_myProvider != null)
            {
                if ( (_myProvider.ContractID != "")&& (_myProvider.ContractID != LastContractID))
                {
                    SqlCommand cmd = new SqlCommand("SELECT * FROM Period WHERE ContrID = " + _myProvider.ContractID, new SqlConnection("Data Source=HOUL-JPIKE3;Initial Catalog=winst;User ID=sa;Password=password;"));
                    cmd.Connection.Open();

                    SqlDataReader ddlValues;
                    ddlValues = cmd.ExecuteReader();

                    myDropDownList.DataSource = ddlValues;
                    myDropDownList.DataTextField = "EndDate";
                    myDropDownList.DataValueField = "PeriodID";
                    myDropDownList.DataBind();

                    LastContractID = _myProvider.ContractID;
                    SetPersonalizationDirty();
                }
                
            }

        }

        void myButton_Click(object sender, EventArgs e)
        {
            PeriodID = myDropDownList.SelectedValue.ToString();
        }

        [ConnectionConsumer("Contract ID Consumer", "ContractIDConsumer")]
        public void ContractIDConsumer(IContractID Provider)
        {
            _myProvider = Provider;
        }

        [ConnectionProvider("Period ID Provider", "PeriodIDProvider")]
        public IPeriodID PeriodIDProvider()
        {
            return this;
        }

    }
}


Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>