Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom control question

Status
Not open for further replies.

tcstom

Programmer
Aug 22, 2003
235
GB
I'm trying to create a custom control library for the first time and I'm a bit stuck. I need a custom control that contains a button which, when clicked, fires a custom event. If it were a user control this would be easy because I could write a Button control into the ascx file and set its event handling accordingly. But with a custom control am I right in saying that the button would have to be written programmatically into the rendered output? If so how can I capture that button's events???
 
You can create the project type "Web Control LIbrary" and then create a class that inherits from the button, then add your own events as you need to.

here is some sample code from a custom button I made - I just added some extra properties, no events.

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TF_LinkButton runat=server></{0}:TF_LinkButton>")]
    public class TF_LinkButton : LinkButton
    {
        private TFConstants functionID = TFConstants.FUNCTION_NOACCESS;
        private AccessConstants privilegeID = AccessConstants.NOACCESS;
        private int iFunctionIDValue = 0;
        private int iPrivilegeIDValue = 0;

        [Bindable(true)]
        [Category("Custom Properties")]
        [DefaultValue(typeof(AccessConstants), "NOACCESS")]
        [Localizable(true)]
        public AccessConstants PrivilegeID
        {
            get
            {
                return privilegeID;
            }

            set
            {
                iPrivilegeIDValue = (int)value;
                privilegeID = value;
            }
        }

        public int PrivilegeIDValue
        {

            get
            {
                return iPrivilegeIDValue;

            }

        }

        [Bindable(true)]
        [Category("Custom Properties")]
        [DefaultValue(typeof(TFConstants), "FUNCTION_NOACCESS")]
        [Localizable(true)]
        public TFConstants FunctionID
        {
            get
            {
                return functionID;
            }

            set
            {
                iFunctionIDValue = (int)value;
                functionID = value;
            }
        }

        public int FunctionIDValue
        {
            get
            {
                return iFunctionIDValue;

            }

        }
    }
}
 
Thanks tperri. However, I'm looking to create a custom control that contains numerous other controls as well as the button. I had tried adding them programmatically in the Render method (and they were not persisting across postbacks) but have since learnt about the AddChildControls method which does allow me to achieve my aim...
 
(Sorry, it's called CreateChildControls)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top