.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Maintaining viewstate on custom controls

Posted By:      Posted Date: May 22, 2011    Points: 0   Category :ASP.Net

Hi Guys,

I created a really simple control that contains a list of filter option controls in much the same way that a listbox has a list of listitems now im trying to get it to maintain the state of its children by serializing key property values of each child in to xml and putting that in to viewstate.

I've been trying to get the concept discussed here http://msdn.microsoft.com/en-us/library/ms973893.aspx which discusses the use of the interface ISerializable to work but asp.net just wont play ball ...

I'm having some trouble tring to get it to serialize in to viewstate as the serializer appears to be trying to serialize the base class properties and basically i end up with errors like - cannot serialize property "page" What do you think is my problem?

The Code:

public class FilterOption : Control, ISerializable
    public event EventHandler Checkchanged;

    CheckBox _chk = new CheckBox();
    Label _lbl = new Label();

    public string Text
        get { return _lbl.Text; }
        set { _lbl.Text = value; }

    public bool Checked
        get { return _chk.Checked; }
        set { _

View Complete Post

More Related Resource Links

Custom controls and ViewState management issue



I want to know that when we create custom controls, then is there State Management through ViewState done automatically or we have to handle it explicitely ?Since i have created Custom Controls that is behaving as if it is not maintaining its state on PostBack.

Custom Controls in C# .NET

Embedding user controls in a Windows form is just like adding a simple button or text box that are already provided with .NET. These basic controls were written essentially like you code your own controls. Typically the controls you design are to be used in multiple forms or to modularize your code. These reasons help reduce the amount of code you have to type as well as make it easier for you to change your implementation. There should almost never be any reason to duplicate code because it leaves a lot of room for bugs. So, implementing functionality specific to your control in the control's source code is a good idea. This reduces code duplication as well as modularize your code, which is a good programming guideline.

Custom Server Control Property not maintaining state


I have created a server control consisting of a TextArea and a div under it.  My goal is to create my own asp:TextArea type control.  the div is for a Character count of the text area and seems to work ok but i added a CharacterCountVisible property to the control that is always returning true.  See Proptery definition below


<Bindable(False), Category("Appearance"), DefaultValue("True"), Localizable(True)> Property CharacterCountVisible() As Boolean
        Return _CountBoxVisible
    End Get
    Set(ByVal value As Boolean)
        _CountBoxVisible = value
    End Set
End Property

Note the _CountBoxVisible variable is define inside the class as :

Dim _CountBoxVisible as Boolean = True

What I am trying to do is give the user/coder the option to show/utilize the char count feature.  Can make it disappear with textarea1.CharacterCountVisible = False but setting it to true will not make it display.


What am i missing





Wicked Code: Craft Custom Controls for Silverlight 2


If you're unfamiliar with Windows Presentation Foundation (WPF), building that first Silverlight custom control can be a daunting experience. This article walks through the process.

Jeff Prosise

MSDN Magazine August 2008

UI on the Fly: Use the .NET Framework to Generate and Execute Custom Controls at Run Time


Creating UI controls on the fly can be accomplished via run-time code generation. And there are lots of reasons to do so. Generating these controls once and then reusing them as needed is more efficient than generating the controls each time. Read on.

Morgan Skinner

MSDN Magazine December 2005

ASP.NET 2.0: Personalize Your Portal with User Controls and Custom Web Parts


ASP.NET 2.0 introduces a Web Part control that is designed to deal with the serialization, storage, and retrieval of customization and personalization data behind the scenes. In this article, the authors explain how you can put the WebPart control to work in your ASP.NET 2.0 applications.

Ted Pattison and Fritz Onion

MSDN Magazine September 2005

How to add designing in Custom Controls ?


Hi ,

I am trying to create Custom control. I have created simple TextBox, Label, Buttons  etc. and added my own logic for their working,  but i want to add  some designing to that controls.I tried to add CSS file for that from the Add New Item, but there is no list for CSS. 

I added forcefully but it is not supporting CSS files.It seems that Custom controls does not support designing and layout through CSS.

Can you tell how can i polish my controls with beautiful designing and layouts ?

viewstate in a custom control

I am having issues with a simple custom control that I created by subclassing the hyperlink control.

I wanted to create a control called BackLink that would automatically grab the referring url to the page when it first loads the page. and on postbacks it would continue to use the initial value

I am able to capture and set the navigateurl property when the containing page first loads just fine but if the page submits to itself, the control loses its navigateurl value.

I have the enableviewstate property set in the aspx page but it doesn't seem to want to keep its viewstate

Am I missing something? Do I have to manage my own ViewState?

here is my code

public class BackLink : System.Web.UI.WebControls.HyperLink

public BackLink()
this.Text = "<< back";

protected override void Render(HtmlTextWriter output)
if (this.Page.IsPostBack == false)
//save back url
Uri _backUri = this.Context.Request.UrlReferrer;

if(_backUri != null) this.NavigateUrl = _backUri.PathAndQuery;

else this.NavigateUrl = "/";

Creating Custom SharePoint 2010 Field Types and Controls with Visual Studio 2010 and Silverlight 3

Learn how developers can create rich experiences in SharePoint Server 2010 publishing sites using Microsoft Silverlight 3.

Video: Creating Custom SharePoint 2010 Field Types and Controls with Visual Studio 2010 and Silverli

Watch the video that accompanies the MSDN article and learn how developers can create rich experiences in SharePoint Server 2010 publishing sites using Microsoft Silverlight 3.

Cannot set custom attributes on mobile controls in this page



I am beginner at mobile Pages with ASP.NET. And i want to make an application with contains  a few TextBox that have different size.

it's my sample code here:

<%@Page Inherits= "System.Web.UI.MobileControls.MobilePage" Language= "C# " %>
<%@ Register TagPrefix= "Mobile" Namespace= "System.Web.UI.MobileControls " Assembly= "System.Web.Mobile" %>
<Mobile:Form runat="server" id="form1" Paginate="true" >
<Mobile:Panel Paginate="True" runat=&

Developing custom controls literature

Hi, I'm looking for some literature which focus on developing custom controls in WPF. It should explain how to develop custom controls in details. It should tell me how to chose which control is the best start point for developing custom control which fits my needs. Next, for example if my new custom control need to be inherited from ItemsControl which methods I should override and what is their purpose, what else I should do etc. What do you recommend? Thanks in advance. Nikola  

Custom panel - missing design time support for controls.

I’m trying to implement a custom panel, one that will behave kind of like the standard Canvas panel. The implementation is done and it is working when I add components in the xaml, but what is not working is move and resize of components in the designer. In the designer each component shows the ‘selected frame’ when selected, but it cannot be moved or resized. Can someone point me to what I’m missing, or an article that describes how do add this support?.   The reason I’m not deriving from the existing Canvas is mainly because I want to understand how it works and how to implement it manually.   Thanks  

Issue with Custom Controls

Hi,we are facing a page loading  problem with custom controls only IE browser. but that the same page open on mozilla firefox 3.5.  we are using all the controls on the page are custom controls. I pasted on custom control code bellow, please verify that is it written proper or not...  using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Web.UI.Design; using System.IO; using System.Configuration; using System.Web.UI.WebControls.WebParts; using System.Drawing.Design; using System.Data; namespace Ays4.Web.UI { #region class CustomizeTextBox [ToolboxData("<{0}:CustomizeTextBox runat=server></{0}:CustomizeTextBox>")] [Designer("System.Web.UI.Design.ReadWriteControlDesigner, System.Design")] [PersistChildren(true)] [ParseChildren(false)] public class CustomizeTextBox : WebControl, INamingContainer { #region Attribute private validateType.ValidationType _ValidationType = validateType.ValidationType.None; private TextBoxType.Mode _Mode = TextBoxType.Mode.None; HtmlTable mainTable; string strJs = ""; private TextBox _inputControl; strin

Controls Disappear in Custom Control Design View

I have a site using eight (so far) custom web user controls  and two of them have started misbehaving in design view.  When designing the control itself some (in one control) or all (in the other) regular controls (labels, text boxes, etc.) disappear from the design view surface.  They can still be seen in the source view and if the control is hosted on a regular .aspx page the control displays properly.  The code window recognizes all controls in the objects dropdown list and I can write appropriate code.  The controls run properly on their hosted pages.  If I switch to Split view and select controls in the source view panel a very small area od the design surface becomes selected (like one pixel for any of us who remember programming old DOS applications with 80-column screens) but no controls can be seen. In short, the controls work in every respect except that I cannot see all or part of the design surface in Design view when editing the controls directly.  I've double-tripple checked the source code for anything that may be causing this and just don't see anything.  (The controls aren't that complicated.)

Isseus with Custom Button Controls

Hi I have inherited a custom button control which was originially derived from a LinkButton, however due to some isseus with the click event and different browsers it has been decided to use a control derived from a Button. I have the following issues with this: 1) The text appears with a box around it whereas the original didn't have this (see example below) 2) When i position the cursor over the button it correctly changes colour but the text disappears whereas the original it remains. Any help on this would be appeciated, I have attached my code and css files Regards Kenpublic class GemaButtonControDesigner : ControlDesigner { public override string GetDesignTimeHtml() { GemaButton control = this.ViewControl as GemaButton; string rtr = control.Text; if (String.IsNullOrEmpty(rtr)) rtr = "GemaButton"; return rtr; } } [ParseChildren(true, "Text")] [Designer(typeof(GemaButtonControDesigner))] [DefaultProperty("Text")] [ToolboxData("<{0}:GemaButton runat=\"server\"></{0}:GemaButton>")] public class GemaButton : Button { #region Constants private const int MINIMUM_SIZE = 16; private const int SMALL_SIZE = 79; private const int DEFAULT_SIZE = 97; private const int SMALL_HEIGHT = 10; private const int DEFAULT_HEIGHT = 14;
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend