Viewstate Issue?

S

SS

Hi,
I've build a custom control that's essentially a header for my web site.
It's pretty simple, with just a property for an image source path, and a
collection of custom objects for links.

The custom class simply has properties for href, target, and name. The
collection implements CollectionBase.

The elements of the collection are persisted as innerdefaultproperties, so
within the tag for the custom control, child elements are present
representing my custom link objects.

The control's render method uses HtmlTextWriter.AddAttribute and
RenderBeginTag/RenderEndTag methods to produce the html. Within the render
method, a foreach loop goes through my collection and renders anchor tags
with the appropriate attributes.

When I place the header control onto a web page, it renders fine. I can edit
the collection of 'links' with the stock design-time editor, and the links
render perfectly when as I add or remove links from the collection.

The problem is, when I do a build on my web app (the page) the page
'refreshes' in the design view, and my links have disappeared. The HTML view
(in VS) still shows the child elements for the links I added, but when I
switch back to design view, they don't render (no errors, just acts like the
don't exist). Indeed, when I click on the "..." to open up the collection
editor, it's empty, even though the HTML is still there.

I haven't even gotten to where I'm rendering this in a browser, since I
can't get it to hold on to links through the build process. Any ideas on
what I'm doing wrong here? I've been thinking that perhaps I need to build
some viewstate management code, but then I also think that this shouldn't be
an issue for the particular problem I'm having -- not getting my collection
to get rerendered in the designer after a build..

Thanks for any help!

Here's my render method:
protected override void Render(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Id,"tblHeader");
output.AddAttribute(HtmlTextWriterAttribute.Border,"0");
output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
output.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
output.RenderBeginTag(HtmlTextWriterTag.Table);

// Begin Table Row
output.RenderBeginTag(HtmlTextWriterTag.Tr);
// Begin Image Cell
output.AddStyleAttribute(HtmlTextWriterStyle.Height,"121");
output.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.AddAttribute(HtmlTextWriterAttribute.Src,this.ImageSrc);
output.AddAttribute(HtmlTextWriterAttribute.Border,"0");
output.AddAttribute(HtmlTextWriterAttribute.Alt,"Home");
output.RenderBeginTag(HtmlTextWriterTag.Img);
output.RenderEndTag();
output.RenderEndTag();
// End Image Cell
// Begin Empty Cell
output.AddStyleAttribute(HtmlTextWriterStyle.Height,"121");
output.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.Write(@" ");
output.RenderEndTag();
// End Empty Cell
// Begin Data Cell
output.AddStyleAttribute(HtmlTextWriterStyle.Height,"121");
output.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.AddAttribute(HtmlTextWriterAttribute.Id,"lblDate");
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.WriteLine(DateTime.Now.ToShortDateString());
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
// End Data Cell

// Begin Table Row
output.RenderBeginTag(HtmlTextWriterTag.Tr);
// Begin Link Cell
output.AddAttribute(HtmlTextWriterAttribute.Colspan,"3");
output.AddAttribute(HtmlTextWriterAttribute.Valign,"top");
output.AddAttribute(HtmlTextWriterAttribute.Align,"left");
output.AddAttribute(HtmlTextWriterAttribute.Class,this.HeaderMenuClass);
output.RenderBeginTag(HtmlTextWriterTag.Td);
foreach (CustomLink objLink in this.Links)
{
output.AddAttribute(HtmlTextWriterAttribute.Class,
(objLink.CssClass.Length > 0) ? objLink.CssClass : this.LinkClass);
output.AddAttribute(HtmlTextWriterAttribute.Href,objLink.NavigateUrl);
output.AddAttribute(HtmlTextWriterAttribute.Target,objLink.Target);
output.AddAttribute(HtmlTextWriterAttribute.Alt, objLink.ToolTip);
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write(objLink.Text);
output.RenderEndTag();
}
output.RenderEndTag();
// End Link Cell
output.RenderEndTag();
// End Table Row
}
 
S

SS

Bah.. Never mind.. Solved by removing the "Set" for the collection and
tweaking the ParseChildren attribute..
Thanks anyway..
 
R

Robert Koritnik

Put PersistChildren and ParseChildren attributes on the class. you can check
the usage on MSDN. Probably this should work fine:
ParseChildren(true),
PersistChildren(false)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,890
Messages
2,569,971
Members
46,306
Latest member
MarcMcEach

Latest Threads

Top