ArticleListing.ascx.cs

Last post 03-27-2008 1:53 PM by Annddrew. 5 replies.

Sort Posts:

  • ArticleListing.ascx.cs

    03-26-2008, 9:10 AM
    • Loading...
    • Annddrew
    • Joined on 02-07-2006, 1:52 PM
    • Posts 60

    I have a question about the code behind of the ArticleListing.ascx.cs user control. What is the purpose of the line of code

    this.Page.RegisterRequiresControlState(this);

    in the Page_Init()?

    It wasn't explained in the book. Thanks

  • Re: ArticleListing.ascx.cs

    03-26-2008, 10:04 AM
    Answer
    • Loading...
    • r_nassabeh
    • Joined on 01-11-2008, 10:14 AM
    • Shiraz, Iran
    • Posts 383

    According to MSDN "Custom server controls that use control state must call the RegisterRequiresControlState method on each request because registration for control state is not carried over from request to request during a postback event. It is recommended that registration occur in the Init event." for more information about Control State take a look at Control state in ASP.NET 2.0

    Good Luck!

    Reza Nassabeh


    Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: ArticleListing.ascx.cs

    03-26-2008, 10:22 AM
    Answer
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 10:51 AM
    • Decatur, IL USA
    • Posts 854

    Sometimes, you need a way for a user control to remember its properties across postbacks, just like a regular control does.

    Controls do this by storing and retrieving their values from the page's state bag. Regular controls use the ViewState. User controls use ControlState.

    This line merely tells the containing page that the ArticleListing.ascx.cs intends to store its properties in the page's ControlState. The actual work of storing and retrieving the values is done in the SaveControlState() and LoadControlState(object SavedState).

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: ArticleListing.ascx.cs

    03-27-2008, 12:14 PM
    • Loading...
    • Annddrew
    • Joined on 02-07-2006, 1:52 PM
    • Posts 60

    So why in the Polls chapter only the PollID property of the PollBox user control is saved to the ControlSate. Why not save the other properties as well such as ShowArchiveLink and ShowHeader etc... ? Thanks

  • Re: ArticleListing.ascx.cs

    03-27-2008, 1:05 PM
    Answer
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 10:51 AM
    • Decatur, IL USA
    • Posts 854

     

    Annddrew:

    So why in the Polls chapter only the PollID property of the PollBox user control is saved to the ControlSate. Why not save the other properties as well such as ShowArchiveLink and ShowHeader etc... ? Thanks

     

    Because the PollID is the only property which actually needs to be stored by the PollBox user control. The other properties refer to "real" server controls inside the user control. The value of these server controls are already stored in Viewstate. For example, look at ShowArchiveLink

     

    public bool ShowArchiveLink
    {
    get { return lnkArchive.Visible; }
    set { lnkArchive.Visible = value; }
    }

    The value of the ShowArchiveLink property is gotten from the value of lnkArchive.Visible. The properties of lnkArchive are already stored in Viewstate.

    PollID, on the other hand, is not connected to any of the server controls, and thus cannot be retrieved from Viewstate. 

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: ArticleListing.ascx.cs

    03-27-2008, 1:53 PM
    • Loading...
    • Annddrew
    • Joined on 02-07-2006, 1:52 PM
    • Posts 60

    Ok thank you very much Lee Dumond. Now I got it.

Page 1 of 1 (6 items)