1

I have a user control in a web part that pulls data from a SP list and displays it in standard ASP.Net controls. When I save the data, the old values seem to get saved even if I had changed them. I debugged it in Visual Studio by attaching to the w3wp.exe process and put a breakpoint on a control reference in the save button commmand. Even though I changed the value in a control, its value in code at the breakpoint was still the old value. I am new to SP webpart development, so maybe I misundertsand something about how the viewstate and postbacks function in this situation.

I used WSPBuilder to create and deply the user control

Thanks.

-- Edit --

Here is the button code. When a breakpoint is inserted, none of the controls (like this.txtNotes.Text) have the new values I just typed in before clicking the save button even though I can see the new value in the UI control:

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ECORepository ecoRepository = new ECORepository();
        ECO eco = new ECO();
        eco.Originator = GetAccountName();
        eco.Chemistry = this.ddlChemistry.SelectedValue;
        eco.inProgressBox = this.inProgressBox.Checked;
        eco.Model = this.txtModel.Text;
        eco.Notes = this.txtNotes.Text;
        eco.ReasonForChange = this.cblReasonForChange.SelectedValue;
        eco.RedLine = this.chkRedLine.Checked;
        eco.Tech = this.ddlTech.SelectedValue;
        eco.Training = this.ddlTraining.SelectedValue;
        if (String.IsNullOrEmpty(this.ecoID))
        {
            this.ecoID = ecoRepository.Add(eco).ToString();
        }
        else
        {
            eco.ECOID = this.ecoID;
            ecoRepository.Edit(eco);
        }
    }

here is the edit method in my repository:

        public void Edit(ECO entity)
        {
            SPSite Mysite = SPContext.Current.Site;
            SPWeb MyWeb = SPContext.Current.Web;
            string WebName = SPContext.Current.Web.Url;

            try
            {
                MyWeb.AllowUnsafeUpdates = true;
                Mysite.AllowUnsafeUpdates = true;
                SPList ecoList = MyWeb.Lists["ECO"];
                SPListItem ecoItem = ecoList.Items.GetItemById(Int32.Parse(entity.ECOID));

                //ecoItem["Originator"] = headerEmptyCheck(entity.Originator);
                ecoItem["Notes"] = headerEmptyCheck(entity.Notes);
                ecoItem["Model"] = headerEmptyCheck(entity.Model);
                ecoItem["Technology"] = headerEmptyCheck(entity.Tech);
                ecoItem["Chemistry"] = headerEmptyCheck(entity.Chemistry);
                ecoItem["Reason for Change"] = headerEmptyCheck(entity.ReasonForChange);
                ecoItem["Training"] = headerEmptyCheck(entity.Training);
                ecoItem["Red Line"] = entity.RedLine;
                ecoItem["Wait for Workflow"] = entity.inProgressBox;
                ecoItem.Update();
            }
            catch (Exception ex)
            {
                return;
            }
        }

finally, here is my ECO class:

    public class ECO
    {
        public string ECOID { get; set; }
        public string Originator { get; set; }
        public string Notes { get; set; }
        public string Model { get; set; }
        public string Tech { get; set; }
        public string Chemistry { get; set; }
        public string ReasonForChange { get; set; }
        public string Training { get; set; }
        public bool RedLine { get; set; }
        public bool inProgressBox { get; set; }
    }

The repository and ECO are nested classes within the user control. I nested them just for testing because I wasnt sure if I could have them as separate classes in a user control in SharePpoint.

3 Answers 3

1

If you will post code you are using we might be able to help you properly but it happened to me when I developed a Visual WebPart in SharePoint 2007 and its properties were keeping getting default after a IIS reset so I am assuming it could be the way you are trying to save values, here's few similar solved questions see if they can help you somehow,

Webpart properties getting empty after IIS Reset

https://stackoverflow.com/questions/3697314/sharepoint-webpart-custom-properties-get-default-values-on-server-reboot

Sharepoint Webpart custom properties get default values on server reboot

Tutorial for Custom Properties of Visual WebParts

Hope it helps.

2
1

iv already addressed this issue here and iv taken it out and reposted it for reference

How to save variables per user session per page

saving session state varibles can be done using:

public string Name
{
    get
    {
        return Page.Session["SaveName"] as string;
    }
    set
    {
        Page.Session["SaveName"] = value;
    }
}

this is a getter/setter to save/get from session state

to use this do the following:

this gets the value from session state (needs to be in onPreRender event to see session state varibles)

txtName.Text = Name;

this sets into session state (can be anywhere you want to save to session state)

Name = txtName.Text;

for int valuse:

cahnge public string Name to public int Name and same goes for anything else like bool values ect.

so it would look like this for int:

public int Name
{
    get
    {
        return Page.Session["SaveName"] as int;
    }
    set
    {
        Page.Session["SaveName"] = value;
    }
}

also just a side note, If you want to get the new values back in you need to follow what i said and do it in the onPreRender method so after postaback or partial it will hit this method and repopulate your controls with the correct values! make sure you have minimal code in your ascx control where it doesnt retain any values as it gets reset to nothing every time its called.

hope it helps :)

4
  • Thanks! I appreciate your help.Although I am a little disappointed that something so simple and fundemental in ASP.Net is Jan 9, 2013 at 13:52
  • Yes its very annoying but what can you do, following the outline steps to save the data you wont run into any more issues it work 100% and its the method i use all the time. As sharepoint is built ontop of asp.net it also has to abide with asp.net rules(alwasy remember asp.net is serverside so postback will lose your data or not save properly ;) ) ahh only if asp.net behaved like winforms it would make programming soooo easy :) i hope my answer has answered your answer ;) simple things in asp.net are neva easy :) iv learnt the hard way lol
    – Ali Jafer
    Jan 9, 2013 at 14:07
  • I think I understand my problem now. In sharepoint, my UC is loaded dynamically. Each time the UC is submitted it is reloaded in the hosting form/webpart, so (like you have noted) the values in the controls of the UC have to be put in the viewstate or some other mechanism so they are retained between postbacks. Does that sound correct? Jan 9, 2013 at 14:59
  • Yes it makes perfect sense! glad to be of assistance! That is how it works unfortunatly, as your probably got your uc loaded in the createchildcontrols it will load every time the page loads (new every time ;) ) so you will need to reload the values saved somewhere like i noted
    – Ali Jafer
    Jan 9, 2013 at 15:06
1

I found my problem. One line of code in the user control page load event solved the problem:

this.ID = "MyControl1";

Assigning an ID to the user control fixed it:

http://bytes.com/topic/asp-net/answers/314816-dynamically-loaded-control-event-only-reached-2nd-postback

The ID is always null when I checked its value on the page load

Your Answer

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

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