• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

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

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!!!
  • 2
1 Solution
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.
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now