Click here to Skip to main content
13,052,494 members (70,254 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 10 Jan 2011

Inherited web control and new properties

, 10 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Inherited web control and new properties

I had to extend the .NET Button control, because I needed to extend it with an extra property 'Tag'. I thought "Must be simple", and wrote the code for the new control:

public class MyButton : Button {
     public string Tag { get; set; }

If you put the control on a page like this:

<myCtrls:MyButton  runat="server" Tag="let's test tag"  önCLick="Btn_OnClick"/>

Then you'll get your Tag in the Btn_OnClick server event.


But if you want to place your inherited control into, say, ListView and assign the Tag to some data from a datasource like

<myCtrls:MyButton  runat="server" Tag="<%# Container.DataItemIndex %>"  önCLick="Btn_OnClick" />

Then you will get null in your server Btn_OnClick event method. I don't know exactly why I get null, because if you pass a constant string value, for example, Tag="777", you will get your "777" on the server side.


You need to modify your auto property in your inherited control in order to get and set your property through the viewstate:

public virtual string Tag
                string s = (string)ViewState["Tag"];
                return s ?? String.Empty;
                ViewState["Tag"] = value;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Russian Federation Russian Federation
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Good to know! 5 Pin
Manfred R. Bihy11-Jan-11 10:09
mvpManfred R. Bihy11-Jan-11 10:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 10 Jan 2011
Article Copyright 2011 by Lionberg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid