locked
Custom Button Property RRS feed

  • Question

  • User32292006 posted

    Hi there,

    i am new to this and i need to add a custom property to the standard Button WebControl. The property would just be of type String which i want to retrieve at runtime like all the other Button properties like ID, Text and so on. Also i want to be able to drag and drop the control from the toolbox like all other controls.

    How can i do this ?

    Thanks a lot !

    Wednesday, March 19, 2008 3:20 AM

Answers

  • User-649410219 posted

    You can use the code bellow.

    If you need keep saved the data you set into properties, you may use ViewState or ControlState.

    This, you must overrides the methods that control the state.

    [ToolboxData("&lt;{0}:MyCustomButton runat=\"server\"&gt;</{0}:MyCustomButton>")]
    public class MyCustomButton : System.Web.UI.WebControls.Button
    {
    private string _MyCustomProperty;

    public string MyCustomProperty
    {
    get{ return _MyCustomProperty; }
    set{ _MyCustomProperty = value; }
    }

    //save the control state protected override object SaveControlState()
    {
    //your code here } //load the control state protected override void LoadControlState(object savedState)
    {
    //your code here } }

    Or, you may use the ViewState.
    [ToolboxData("&lt;{0}:MyCustomButton runat=\"server\"&gt;</{0}:MyCustomButton>")]
    public class MyCustomButton : System.Web.UI.WebControls.Button
    {
    private string _MyCustomProperty;

    public string MyCustomProperty
    {
    get{ if(ViewState["MyCustomProperty"] != null) return ViewState["MyCustomProperty"].ToString(); else return string.Empty; }
    set{ ViewState.Add("MyCustomProperty", value); }
    }
    }
     When you generate your control's assembly (dll), in ToolBox Tab you add it. And then, very easy, drag'n drop!

    I hope it helps u!
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 19, 2008 10:52 AM