Click here to Skip to main content
Click here to Skip to main content

Tagged as

Inherited web control and new properties

, 10 Jan 2011 CPOL
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.

PROBLEM

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.

SOLUTION

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
{
           get
           {
                string s = (string)ViewState["Tag"];
                return s ?? String.Empty;
            }
            set
            {
                ViewState["Tag"] = value;
            }
}

License

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

Share

About the Author

Lionberg

Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 Good to know! 5 PinmvpManfred R. Bihy11-Jan-11 11:09 

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

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

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