I was working on a Silverlight web part for SharePoint 2010 and encountered the need to do this. I had a div tag, floating on top of my Silverlight for displaying HTML content, and had the need to update the HTML from within my Silverlight code.
This is how I went about doing it:
- I was using a label to display the HTML content so I had something like
- Make sure you have the HTML content you want to update in the element. I was using content from a Rich Text Field in a SharePoint list. We’ll call the list item that contains the content “Item”.
Add a similar line of code to your Silverlight application where you want to update your HTML element:
- Replace FieldName above with whatever the name of the column in your SharePoint list is. You could also replace Item[“FieldName”].ToString() with whatever string of HTML you wish to display.
- That should be it. Whenever that line of code is called, your HTML element will be updated.
A couple of notes I also discovered while working on this.
- There is also a .SetAttribute() that I tried first, however, this only seems to work with Internet Explorer from what I found. Once I found that .SetProperty worked for all browsers I didn’t look any further into .SetAttribute
A third method that exists is .SetStyleAttribute(). This can be used to set any CSS Styling for the HTML element. This also works with all browsers. An example of this would be: