0

not sure if this is even possible. What I'm trying to do is create a visual web-part for SharePoint 2010 (using Visual Studio 2013) that will allow the user to add the web-part to a page then add data to a text box, the data is then saved with the web-parts variables.

What I've tried so far is adding a button to the page that when clicked will set the variable but this doesn't seem to work as the page refreshes, which resets the content in the text-boxes to the stored data before the code can override it.

Does anyone know if this is even possible? Any help on this would be greatly appreciated as I've hit a brick wall. Cheers.

2 Answers 2

1

I am not exactly sure what you mean by "webpart variable". When you click a button that causes a postback(the pages refreshes), you maybe reset your value in the textbox. What you can do is to set your textbox value only if it is not a postback:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
      myTextBox.Text = GetTheValue();
  }
}

If it isn't what the problem is, please let me know.

2
  • Thanks for the reply, sorry by webpart variable I was talking about the properties of the webpart ` [WebBrowsable(true), Category("Accordion Data"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Section 1 Title")] public String Section01Title { get; set; } ` What I'm trying to do is when my button is clicked set the string 'Section01Title' to what is entered in a textbox. I've tried the way you mentioned (Section01Title = txtBox.Text) but unfortunately this didn't work.
    – William
    Commented Feb 20, 2013 at 10:09
  • sorry the code I pasted made that look like a really messy response
    – William
    Commented Feb 20, 2013 at 10:13
0

For anyone that is wanting it I managed to figure it out.

protected void Button1_Click(object sender, EventArgs e)
    {
        Section01Title = txtTitle01.Text;
        Section01Content = txtContent01.Text;
        this.SetPersonalizationDirty();
    }

I was missing the "this.SetPersonalizationDirty();" line after setting the Sharepoint properties. Apparently this tells the web-part to save back to Sharepoint (from what I understand)

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.