I will beat around the bush and guess that the viewer is nuking the map when it loads. I ran into that issue when inserting a layer on the initial load for markers. This is what I hacked together, search for the map.Create and replace that line with this:
{code}
// you need to modify the viewer source in mapframe.aspx to try and open and existing map instead of just creating it
if(resourceSrvc.ResourceExists(new MgResourceIdentifier("Session:" + sessionId + "//" + mapName + "." + MgResourceType.Map)))
map.Open(resourceSrvc, mapName);
else
map.Create(resourceSrvc, resId, mapName);
{code}
You will need to call your own map.Create() (which I can assume you are doing since you are modifying the map/layers).