locked
Need some custom properties in server controls in asp.net. RRS feed

  • Question

  • User-1976290245 posted

    I Need some custom properties in server  controls in asp.net.
    e.g., i need a property ThemeID in asp.net Button control .
    How do i inherit Button class so that i can use that control in aspx page to declaratively use my Button control ?

    Monday, October 10, 2011 12:08 AM

Answers

  • User3866881 posted

    Can  i create that server control in the Code Behind itself ?Because that button will be used in that single page .

    If possible how can i achieve that ? Please help !

    Of course you can!

    Please create a common class file (with extesion cs) and write a class by your own, and then the class must inherit System.Web.Control, becuase this is the most basic class to be used if you want to create a Server-side class. And I'd like to share my demo to you, this is a demo telling you how to customized a TextBox, I've also defined a public property to tell whether it supports "AutoPostBack" or not——Just like what you can see in the ToolBox's one——

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;

    namespace SimpleCustomizedControl
    {
        public class MyTextBoxParameter
        {
            public string Name { get; set; }
            public string Value { get; set; }
        }

        public class MyTextBox:System.Web.UI.Control,IPostBackDataHandler
        {
            /// <summary>
            /// Event's declaration for TextBox's value changed 
            /// </summary>
            public event Action<MyTextBoxParameter> TextChangedEvent;
            /// <summary>
            /// The class that carries Name and Value for TextBox
            /// </summary>
            private MyTextBoxParameter mp = null;
            /// <summary>
            /// Supoort PostBAck or Not, this is a self-made property
            /// </summary>
            public bool IsAutoPostBack
            {
                get
                {
                    return ViewState["flag"] == null ? false : Convert.ToBoolean(ViewState["flag"]);
                }
                set
                {
                    ViewState["flag"] = value;
                }
            }

            /// <summary>
            /// Keep the value between posting and receiving, because HTML is of no memory
            /// </summary>
            public string Text
            {
                get
                {
                    return ViewState["txt"] == null ? string.Empty : ViewState["txt"].ToString();
                }
                set
                {
                    ViewState["txt"] = value;
                }
            }

            /// <summary>
            /// Accept the postback's values when doing post back.
            /// </summary>
            /// <param name="postDataKey">Name</param>
            /// <param name="postCollection">All the HTML controls' Name if same</param>
            /// <returns></returns>
            public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
            {
                string value = postCollection[postDataKey];
                if (string.IsNullOrEmpty(value) || IsAutoPostBack==false)
                {
                    return false;
                }
                else
                {
                  mp =  new MyTextBoxParameter { Name = postDataKey, Value = value };
                  Text = mp.Value;
                    return true;
                }
            }

            /// <summary>
            /// If LoadPostData=true, this event get fired
          /// </summary>
            public void RaisePostDataChangedEvent()
            {
                TextChangedEvent(mp);
            }
            protected override void Render(HtmlTextWriter writer)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "pink");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "blue");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "green");
                writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
                writer.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
                if (IsAutoPostBack)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Onchange, "document.forms[0].submit();");
                }
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 11, 2011 3:22 AM
  • User-16411453 posted

    All a button control does is create an <input> tag for a button. You can alter the buttons look and feel by using css. The button control will be limited to what you can do in html. So a button user or server control written to your specs, with just include a Property, tied to an enumerator, with an allowable list of themes. Then you have to decide, once the theme is selected, how to change the look and feel of the css. Well now your back to square1, css.

    The short answer is, use the theme mechanism and tie it to the button.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 11, 2011 10:56 PM

All replies

  • User-1976290245 posted

    Is it necessery to create a custom server control in my case ?

    is there any another way ?

    Monday, October 10, 2011 12:29 AM
  • User-1976290245 posted

    Can  i create that server control in the Code Behind itself ?Because that button will be used in that single page .

    If possible how can i achieve that ? Please help !

    Monday, October 10, 2011 1:12 AM
  • User3866881 posted

    Can  i create that server control in the Code Behind itself ?Because that button will be used in that single page .

    If possible how can i achieve that ? Please help !

    Of course you can!

    Please create a common class file (with extesion cs) and write a class by your own, and then the class must inherit System.Web.Control, becuase this is the most basic class to be used if you want to create a Server-side class. And I'd like to share my demo to you, this is a demo telling you how to customized a TextBox, I've also defined a public property to tell whether it supports "AutoPostBack" or not——Just like what you can see in the ToolBox's one——

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;

    namespace SimpleCustomizedControl
    {
        public class MyTextBoxParameter
        {
            public string Name { get; set; }
            public string Value { get; set; }
        }

        public class MyTextBox:System.Web.UI.Control,IPostBackDataHandler
        {
            /// <summary>
            /// Event's declaration for TextBox's value changed 
            /// </summary>
            public event Action<MyTextBoxParameter> TextChangedEvent;
            /// <summary>
            /// The class that carries Name and Value for TextBox
            /// </summary>
            private MyTextBoxParameter mp = null;
            /// <summary>
            /// Supoort PostBAck or Not, this is a self-made property
            /// </summary>
            public bool IsAutoPostBack
            {
                get
                {
                    return ViewState["flag"] == null ? false : Convert.ToBoolean(ViewState["flag"]);
                }
                set
                {
                    ViewState["flag"] = value;
                }
            }

            /// <summary>
            /// Keep the value between posting and receiving, because HTML is of no memory
            /// </summary>
            public string Text
            {
                get
                {
                    return ViewState["txt"] == null ? string.Empty : ViewState["txt"].ToString();
                }
                set
                {
                    ViewState["txt"] = value;
                }
            }

            /// <summary>
            /// Accept the postback's values when doing post back.
            /// </summary>
            /// <param name="postDataKey">Name</param>
            /// <param name="postCollection">All the HTML controls' Name if same</param>
            /// <returns></returns>
            public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
            {
                string value = postCollection[postDataKey];
                if (string.IsNullOrEmpty(value) || IsAutoPostBack==false)
                {
                    return false;
                }
                else
                {
                  mp =  new MyTextBoxParameter { Name = postDataKey, Value = value };
                  Text = mp.Value;
                    return true;
                }
            }

            /// <summary>
            /// If LoadPostData=true, this event get fired
          /// </summary>
            public void RaisePostDataChangedEvent()
            {
                TextChangedEvent(mp);
            }
            protected override void Render(HtmlTextWriter writer)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "pink");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "blue");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
                writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "green");
                writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
                writer.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
                if (IsAutoPostBack)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Onchange, "document.forms[0].submit();");
                }
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 11, 2011 3:22 AM
  • User-16411453 posted

    All a button control does is create an <input> tag for a button. You can alter the buttons look and feel by using css. The button control will be limited to what you can do in html. So a button user or server control written to your specs, with just include a Property, tied to an enumerator, with an allowable list of themes. Then you have to decide, once the theme is selected, how to change the look and feel of the css. Well now your back to square1, css.

    The short answer is, use the theme mechanism and tie it to the button.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 11, 2011 10:56 PM