Inherited Web User Control With a Custom Property doesn't Work.

Posted on 2005-04-15
Last Modified: 2012-06-27
Hi all.

My Asp.Net application generate a data list witch checkboxes. I created a inherited check box control, that has a custom string property called TagS. The code of my inherited control is:

Public Class ASPCheckBox
    Inherits System.Web.UI.WebControls.CheckBox

    Private vTagS As String          'Variable the Holds my Tag.Value

    Public Sub New()
    End Sub

    'Component overrides dispose to clean up the component list.
    Protected Overloads Sub Dispose()
    End Sub
    Public Property TagS() As String
            Return vTagS
        End Get
        Set(ByVal Value As String)
            vTagS = Value
        End Set
    End Property

End Class

So, in my code, i create a new ASPCheckBox componet as this:
    'Create a Check Box on Each Item created on the DataList
    Private Sub AddCheckBoxesModulesList(ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)

        Dim cb As New ASPCheckBox

        cb.EnableViewState = True
        cb.AutoPostBack = False
        cb.ID = e.Item.ItemIndex
        cb.CssClass = "DefaultTextSmall"
        cb.Width = New System.Web.UI.WebControls.Unit(200)

        'Define Handler for CheckBox
        AddHandler cb.CheckedChanged, AddressOf OnCheckedChangedEventModulesList
        e.Item.Controls.Add(cb)    'Add the control to the List

    End Sub

And on the ItemDataBound Method a set the TagS Property

    Private Sub ModulesList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles ModulesList.ItemDataBound

        Dim DbRow As System.Data.DataRow
        Dim cb As ASPCheckBox
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            'Busca a linha que esta lincada à esse item
            DbRow = CType(CType(CType(CType(e.Item, System.Web.UI.WebControls.DataListItem).DataItem, Object), System.Data.DataRowView).Row, System.Data.DataRow)
            cb = CType(e.Item.Controls(0), ASPCheckBox)

            'Texto do Combo
            cb.Text = DbRow.Item(1)
            cb.TagS = DbRow.Item(0)
   End Sub

When the user click on the Submit button, i need to check the value of the TagS property of each check box added on each DataListItem, so...

    Private Sub OnCheckedChangedEventModulesList(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim cb As ASPCheckBox
        Dim x As DataListItem

        For Each x In ModulesList.Items

            cb = CType(x.Controls(0), ASPCheckBox)

            if cb.TagS = "A" then
            end if

    End Sub

But. all other default controls properties is ok, except my custom property TagS that show = Notinhg...

Does anybody knows what is wrong and how could i solve this?

Thanks a lot!!!
Question by:regisdaniel
    LVL 12

    Expert Comment

    I think that's because your added property is not write to viewstate and read from viewstate on postback.

    You should override the SaveViewState and LoadViewState methods.
    LVL 12

    Accepted Solution

    An example:(c#)

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.IO;
    using System.Text;  
    using System.Collections;

    namespace PAB.WebControls {
     /// <summary>
     /// StateList
     /// Simple control that renders  a list of State Names and codes.
     /// </summary>
     [ToolboxData("<{0}:StateList runat=server></{0}:StateList>")]
     public class StateList : System.Web.UI.WebControls.DropDownList
      internal bool IsLicensed=false;
      public StateList()
      {  //you can comment this out starting here--
       PAB.WebControls.Licensing lic=new PAB.WebControls.Licensing();
         this.Items.Add(new ListItem("NOT LICENSED", "NOT LICENSED"));// -- and ending here to remove reference to licensing

       for(int i=0;i<StateCode.Length;i++)
        this.Items.Add (new ListItem(StateName[i],StateCode[i]));
      }private string[] StateCode =
      private  string[] StateName =
       "Alabama","Kentucky","North Dakota","Alaska","Louisiana","Ohio","Arizona",
       "Pennsylvania","Colorado","Michigan","Rhode Island","Connecticut","Minnesota",
       "South Carolina","Delaware","Mississippi","South Dakota","District Columbia",
       "Hawaii","Nevada","Vermont","Iowa","New Hampshire","Virginia","Idaho","New Jersey",
       "Washington","Illinois","New Mexico","West Virginia","Indiana","New York","Wisconsin",
       "Kansas","North Carolina","Wyoming","American Samoa","Marshall Islands","Puerto Rico",
       "Federated States","Micronesia","N Mariana Islands","Virgin Islands","Guam","Palau"};
      /// <summary>
      /// Override to save data to viewstate
      /// </summary>
      protected override object SaveViewState()
       // create object array for Item count + 1
       object[] allCountries = new object[this.Items.Count + 1];

       // the +1 is to hold the base info
       object baseState = base.SaveViewState();
       allCountries[0] = baseState;    
       return allCountries;
      /// <summary>
      /// Override to restore ViewState
      /// </summary>
      protected override void LoadViewState(object savedState)
       if (savedState != null)
        object[] myState = (object[])savedState;
        // restore base first
        if (myState[0] != null)
      /// <summary>
      /// Override to render contents of dropdownlist
      /// </summary>
      protected override void RenderContents(HtmlTextWriter writer)
       foreach(ListItem li in this.Items)

        if(li.Attributes.Count > 0)


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now