locked
Composite Custom Control Attributes RRS feed

  • Question

  • User-1438843037 posted

     

     I have Custom Control contain Label and TextBox, How i can add all unhandled attributes to the TextBox.

     

     <MyCustom:InputField ID="Code" runat="server" Text="" onchange="alert('asdf');"></MyCustom:InputField>

     

    onchange Attributes currently added on the span that contain Label and TextBox, but i want it on TextBox Only,

     

    Thanks in advanced

     

    Monday, November 30, 2009 4:16 PM

Answers

  • User2089561920 posted

    hmm the only way to do that would be to inherit from textbox and override the onclick event.

    alternatively you will have to manually expose all the events you want in the textbox and image controls inside your custom control

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 2, 2009 7:44 PM

All replies

  • User2089561920 posted

    hi,

    is it possible to show your code for the custom control?


    alternatively another way is to create a new control which inherits from textbox, then you overwrite the render function to render the label first before the textbox

    this way all events and attributes follow the textbox

    Monday, November 30, 2009 4:56 PM
  • User-1438843037 posted
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    
    namespace MyCustom
    {
     
        [Localizable(true)]
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:InputField runat=server></{0}:InputField>")]
        
        public class InputField : System.Web.UI.WebControls.WebControl
        {
            
            TextBox txt = new TextBox();
            Label lbl = new Label();
            HtmlImage Search;
            Boolean _Mandatory;
            Boolean _DefaultCursor;
            string _ResourceCode;
            
            public enum TextBoxStyles
            {
                TextBox,
                Calender,
                TextBoxWithSearch
            }
            TextBoxStyles _Style;
            
            /// <summary>
            /// constructor
            /// </summary>
            public InputField()
            {
                base.Width = new Unit("100%");
                _ResourceCode = this.ID;
                txt.Width = new Unit("75%");
                lbl.Width = new Unit("25%");
                this.EnableViewState = false;
                //_Style = TextBoxStyles.TextBox;  
            }
    
            [Bindable(true), Category("PROPERTY CATEGORY"), BrowsableAttribute(true), DefaultValue("TextBox")]
            public TextBoxStyles TextBoxStyle
            {
                get { return _Style; }
                set { _Style = value; }
            }
    
            [Bindable(true), Category("PROPERTY CATEGORY"), BrowsableAttribute(true), DefaultValue("False")]
            public Boolean DefaultCursor
            {
                get { return _DefaultCursor; }
                set { _DefaultCursor = value; }
            }
    
            [Bindable(true), Category("PROPERTY CATEGORY"), BrowsableAttribute(true), DefaultValue("False")]
            public Boolean MandatoryField
            {
                get { return _Mandatory; }
                set { _Mandatory = value; }
            }
    
            [Bindable(true), Category("PROPERTY CATEGORY"), BrowsableAttribute(true), DefaultValue("")]
            public string ResourceCode
            {
                get { return _ResourceCode == null || _ResourceCode.Length == 0 ? "" : _ResourceCode; }
                set { _ResourceCode = value; }
            }
            MySession objUser = (MySession)(HttpContext.Current.Session["UserSession"]);
            /// <summary>
            /// call base class method
            /// additional code when control raise event prerender
            /// </summary>
            /// <param name="e"></param>
            protected override void OnPreRender(EventArgs e)
            {
                if (!this.DesignMode)
                {
                    switch (_Style)
                    {
                        case TextBoxStyles.Calender:
                            CalendarPreRender();
                            break;
                        case TextBoxStyles.TextBoxWithSearch:
                            break;
                    }
                }
                if (ResourceCode != null && ResourceCode.Length > 0)
                {
                    string LanguageText = LangReader.GetLangWTT(ResourceCode);
                    this.lbl.Text = LanguageText.Split(new string[] { "||~~||" }, StringSplitOptions.None)[0];
                    string ToolTip = LanguageText.Split(new string[] { "||~~||" }, StringSplitOptions.None)[1];
                    this.ToolTip = (ToolTip == "" ? this.lbl.Text : ToolTip);
                }
                if (_Mandatory)
                {
                    if (objUser != null)
                        this.lbl.Style.Add("color", objUser.MandatoryColor);
                    else
                        this.lbl.Style.Add("color", "red");
                    this.txt.BackColor = System.Drawing.Color.GhostWhite;
                }
                base.OnPreRender(e);
            }
    
            void CalendarPreRender()
            {
                //register the Embedded javascript file
    
                Page.Response.Write("<script type=\"text/javascript\">$(function() {$('#" + this.txt.ClientID + "').datepicker({ dateFormat: 'dd/mm/yy',showAnim: 'fold'  });})</script>");
                this.txt.MaxLength = 10;
               
                this.txt.Style.Add("background-image", "url(" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyCustom.Calendar.jpg") + ")");
                this.txt.Style.Add("background-repeat", "no-repeat");
                if (objUser != null && objUser.Language == Languages.Arabic)
                    this.txt.Style.Add("background-position", "left");
                else
                    this.txt.Style.Add("background-position", "right");
                
            }
            void TextBoxWithSearchRender()
            {
                Search = new HtmlImage();
                Search.Attributes.Add("class", this.CssClass+ "img");
                
                
            }
            /// <summary>
            /// call base class method
            /// additional code when control raise event Render 
            /// </summary>
            /// <param name="writer"></param>
            protected override void Render(System.Web.UI.HtmlTextWriter writer)
            {
                if (!this.DesignMode)
                {
                    switch (_Style)
                    {
                        case TextBoxStyles.Calender:
                            break;
                        case TextBoxStyles.TextBoxWithSearch:
                            TextBoxWithSearchRender();
                            break;
                    }
                }
                if (_DefaultCursor)
                {
                    Page.Response.Write("<script type=\"text/javascript\">$(function() {$('#" + this.txt.ClientID + "').focus();})</script>");
                }
                base.Render(writer);
            }
    
    
            /// <summary>
            /// call base class method
            /// additional code when control raise event CreateChildControls
            /// </summary>
            protected override void CreateChildControls()
            {
                Controls.Clear();
                txt.ID = "txt" + this.ID;
                lbl.ID = "lbl" + this.ID;
                Controls.Add(lbl);
                Controls.Add(txt);
                if (this.TextBoxStyle == TextBoxStyles.TextBoxWithSearch && Search != null)
                {
                    Search.ID = "img" + this.ID;
                    Controls.Add(Search);
                }
            }
    
            /// <summary>
            /// call base class method
            /// additional code when control raise event OnInit
            /// </summary>
            /// <param name="e"></param>
            public override Unit Width
            {
                get { return base.Width; }
                set { base.Width = value; }
            }
    
            public string Text
            {
                get { return txt.Text; }
                set { txt.Text = value; }
            }
    
            /// <summary>
            /// call base class method
            /// additional code when control raise event OnInit
            /// </summary>
            /// <param name="e"></param>
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                //register the Embedded javascript file
                this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "TextBoxRegKey", Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyCustom.TextBox.Script.js"));
    
            }
        }
    }
    


     

     

    Above is my custom control code, and thanks for your alternatives but for example i want to assign for example "onclick" attribute to Image with custom control and assign all other attributes to textBox. is there ability to handle all attributes in Custom control??

    Wednesday, December 2, 2009 1:25 PM
  • User2089561920 posted

    hmm the only way to do that would be to inherit from textbox and override the onclick event.

    alternatively you will have to manually expose all the events you want in the textbox and image controls inside your custom control

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 2, 2009 7:44 PM