Viewstate Issue?

Discussion in 'ASP .Net Web Controls' started by SS, Jun 23, 2004.

  1. SS

    SS Guest

    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
    }
     
    SS, Jun 23, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Rush
    Replies:
    2
    Views:
    567
    Ben Rush
    Dec 5, 2003
  2. Steph
    Replies:
    2
    Views:
    1,695
    heislord5
    May 11, 2011
  3. ujjc001
    Replies:
    0
    Views:
    1,397
    ujjc001
    Jul 27, 2005
  4. sck10
    Replies:
    6
    Views:
    955
    sck10
    Sep 1, 2006
  5. mark

    Viewstate errors... how do I get viewstate working?

    mark, Feb 20, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    299
Loading...

Share This Page