Custom WebContol DropDownList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harshmaul
    Recognized Expert Contributor
    • Jul 2007
    • 490

    Custom WebContol DropDownList

    Hi, first let me post my code....

    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 SPGMediaWebControls
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:ErrorDropDownList runat=server></{0}:ErrorDropDownList>")]
        public class ErrorDropDownList : DropDownList
        {
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
            public string Text
            {
                get
                {
                    String s = (String)ViewState["Text"];
                    return ((s == null) ? String.Empty : s);
                }
    
                set
                {
                    ViewState["Text"] = value;
                }
            }
            private string errorMsg;
    
            public string ErrorMsg
            {
                get { return errorMsg; }
                set { errorMsg = value; }
            }
            public override void RenderEndTag(HtmlTextWriter writer)
            {
                base.RenderEndTag(writer);
                writer.WriteEndTag("div");
            }
            public override void RenderBeginTag(HtmlTextWriter writer)
            {
                errorMsg = errorMsg == null ? "" : errorMsg;
                string errorCSS = "errorCSS";
                if (errorMsg.Length > 1)
                {
                    writer.WriteBeginTag("div class='" + errorCSS + "' style='border:2px solid #0000ff'>");
                }
                else
                {
                    errorMsg = "";
                    writer.WriteBeginTag("div>");
                }
                base.RenderBeginTag(writer);
                writer.Write(errorMsg);
                
            }
            protected override void RenderContents(HtmlTextWriter output)
            {
                output.Write(Text);
            }
        }
    }
    I'm trying to override the drop down list control so that i can standardise validation throughout the all of our user interfaces.

    However i can't work out how to add ListItems to this. Some please shoot me, and if you can't do that please tell me what i'm doing wrong.

    Thanks.
  • harshmaul
    Recognized Expert Contributor
    • Jul 2007
    • 490

    #2
    Hi all, i solved it, incase your interested.... heres the final code...


    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 MyWebControls
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:ErrorDropDownList runat=server></{0}:ErrorDropDownList>")]
        public class ErrorDropDownList : System.Web.UI.WebControls.DropDownList
        {
    
            [
    
            Bindable(true)]
    
            private string errorMsg;
    
            public string ErrorMsg
            {
                get { return errorMsg; }
                set { errorMsg = value; }
            }
            public override void DataBind()
            {
                base.DataBind();
            }
    
            public override void RenderBeginTag(HtmlTextWriter writer)
            {
                            errorMsg = errorMsg == null ? "" : errorMsg;
                string errorCSS = "errorCSS";
                if (errorMsg.Length > 1)
                {
                    writer.WriteBeginTag("div class='" + errorCSS + "' style='border:2px solid #0000ff'>");
                }
                else
                {
                    errorMsg = "";
                    writer.WriteBeginTag("div>");
                }
                base.RenderBeginTag(writer);
            }
            public override void RenderControl(HtmlTextWriter writer)
            {
                base.RenderControl(writer);
            }
            public override void RenderEndTag(HtmlTextWriter writer)
            {
                base.RenderEndTag(writer);
                            writer.Write(errorMsg);
                writer.WriteEndTag("div");
            }
    
        }
    }

    You can give it a error message and then write a style for the CSS class "errorCSS".

    Comment

    Working...