User Control's Default Property Values do Not Appear In Properties Window

Discussion in 'ASP .Net' started by Jonathan Wood, Feb 7, 2008.

  1. Below is a user control (ascx file) I created.

    Everything works fine except, when I first place an instance of the control
    on a form and look at the associated properties, my initial property values
    appear blank. They only show a value if I enter a new value.

    What is the trick to have my default property values appear in the
    properties window?

    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    <%@ Control Language="C#" ClassName="HelpButton" %>

    <script runat="server">

    protected string _helpFile = "Help.aspx";
    protected string _imageUrl = "Help.png";

    public string HelpFile
    {
    get { return _helpFile; }
    set { _helpFile = value; }
    }

    public string ImageURL
    {
    get { return _imageUrl; }
    set { _imageUrl = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    RegisterClientScripts();
    }

    protected override void Render(HtmlTextWriter writer)
    {
    base.Render(writer);

    // Render control
    writer.Write(String.Format("<input type=\"image\" src=\"{0}\" alt=\"Help\"
    onclick=\"javascript:return ShowHelp('{1}');\" style=\"border-width:0px;\",
    />",
    ResolveUrl("~/Images/Help.jpg"), ResolveUrl("~/Help/" + HelpFile)));
    }

    protected void RegisterClientScripts()
    {
    // Create client-side scripts to generate kilograms from pounds and vice
    versa
    if (!Page.ClientScript.IsClientScriptBlockRegistered("ShowHelp"))
    {
    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ShowHelp",
    "function ShowHelp(url){" +
    "window.open(url, \"_blank\",
    \"height=300,width=450,toolbar=no,status=no,menubar=no,location=no,scrollbars=yes\");"
    +
    "return false;}", true);
    }
    }

    </script>
     
    Jonathan Wood, Feb 7, 2008
    #1
    1. Advertising

  2. Jonathan Wood

    Manish Guest

    RE: User Control's Default Property Values do Not Appear In Properties

    Hi Jonathan,

    Please refer to the code below to create the CotnrolLibrary and then use it
    in your App.

    namespace WebControlLibrary1
    {
    /// <summary>
    /// Summary description for WebCustomControl1.
    /// </summary>
    [DefaultProperty("Text"),
    ToolboxData("<{0}:popupGreeting runat=server></{0}:popupGreeting>")]
    public class PopupGreeting : System.Web.UI.Control
    {
    [Bindable(true),
    Category("Appearance"),
    DefaultValue("")]
    public string PopupMessage
    {
    get
    {
    // See if the item exists in the ViewState
    object popupMessage = this.ViewState["PopupMessage"];
    if (popupMessage != null)
    return this.ViewState["PopupMessage"].ToString();
    else
    return "Welcome to my Web site!";
    }

    set
    {
    // Assign the ViewState variable
    ViewState["PopupMessage"] = value;
    }
    }

    [Bindable(true),
    Category("Appearance"),
    DefaultValue("")]
    public bool Enabled
    {
    get
    {
    // See if the item exists in the ViewState
    object enabled = this.ViewState["Enabled"];
    if (enabled != null)
    return (bool) this.ViewState["Enabled"];
    else
    return true;
    }

    set
    {
    // Assign the ViewState variable
    ViewState["Enabled"] = value;
    }
    }


    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);

    string scriptKey = "intoPopupMessage:" + this.UniqueID;

    if (!Page.IsStartupScriptRegistered(scriptKey) && this.Enabled &&
    !Page.IsPostBack)
    {
    string scriptBlock =
    @"<script language=""JavaScript"">
    <!--
    alert(""%%POPUP_MESSAGE%%"");
    // -->
    </script>";
    scriptBlock = scriptBlock.Replace("%%POPUP_MESSAGE%%", this.PopupMessage);

    Page.RegisterStartupScript(scriptKey, scriptBlock);
    }
    }
    }

    }

    Regards,
    Manish
    www.ComponentOne.com

    "Jonathan Wood" wrote:

    > Below is a user control (ascx file) I created.
    >
    > Everything works fine except, when I first place an instance of the control
    > on a form and look at the associated properties, my initial property values
    > appear blank. They only show a value if I enter a new value.
    >
    > What is the trick to have my default property values appear in the
    > properties window?
    >
    > Thanks.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    > <%@ Control Language="C#" ClassName="HelpButton" %>
    >
    > <script runat="server">
    >
    > protected string _helpFile = "Help.aspx";
    > protected string _imageUrl = "Help.png";
    >
    > public string HelpFile
    > {
    > get { return _helpFile; }
    > set { _helpFile = value; }
    > }
    >
    > public string ImageURL
    > {
    > get { return _imageUrl; }
    > set { _imageUrl = value; }
    > }
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > RegisterClientScripts();
    > }
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > base.Render(writer);
    >
    > // Render control
    > writer.Write(String.Format("<input type=\"image\" src=\"{0}\" alt=\"Help\"
    > onclick=\"javascript:return ShowHelp('{1}');\" style=\"border-width:0px;\",
    > />",
    > ResolveUrl("~/Images/Help.jpg"), ResolveUrl("~/Help/" + HelpFile)));
    > }
    >
    > protected void RegisterClientScripts()
    > {
    > // Create client-side scripts to generate kilograms from pounds and vice
    > versa
    > if (!Page.ClientScript.IsClientScriptBlockRegistered("ShowHelp"))
    > {
    > Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ShowHelp",
    > "function ShowHelp(url){" +
    > "window.open(url, \"_blank\",
    > \"height=300,width=450,toolbar=no,status=no,menubar=no,location=no,scrollbars=yes\");"
    > +
    > "return false;}", true);
    > }
    > }
    >
    > </script>
    >
    >
     
    Manish, Feb 7, 2008
    #2
    1. Advertising

  3. Re: User Control's Default Property Values do Not Appear In Properties

    Okay, looks like I may be missing a few elements. I'll print out your post
    and do a bit more studying.

    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "Manish" <> wrote in message
    news:...
    > Hi Jonathan,
    >
    > Please refer to the code below to create the CotnrolLibrary and then use
    > it
    > in your App.
    >
    > namespace WebControlLibrary1
    > {
    > /// <summary>
    > /// Summary description for WebCustomControl1.
    > /// </summary>
    > [DefaultProperty("Text"),
    > ToolboxData("<{0}:popupGreeting runat=server></{0}:popupGreeting>")]
    > public class PopupGreeting : System.Web.UI.Control
    > {
    > [Bindable(true),
    > Category("Appearance"),
    > DefaultValue("")]
    > public string PopupMessage
    > {
    > get
    > {
    > // See if the item exists in the ViewState
    > object popupMessage = this.ViewState["PopupMessage"];
    > if (popupMessage != null)
    > return this.ViewState["PopupMessage"].ToString();
    > else
    > return "Welcome to my Web site!";
    > }
    >
    > set
    > {
    > // Assign the ViewState variable
    > ViewState["PopupMessage"] = value;
    > }
    > }
    >
    > [Bindable(true),
    > Category("Appearance"),
    > DefaultValue("")]
    > public bool Enabled
    > {
    > get
    > {
    > // See if the item exists in the ViewState
    > object enabled = this.ViewState["Enabled"];
    > if (enabled != null)
    > return (bool) this.ViewState["Enabled"];
    > else
    > return true;
    > }
    >
    > set
    > {
    > // Assign the ViewState variable
    > ViewState["Enabled"] = value;
    > }
    > }
    >
    >
    > protected override void OnPreRender(EventArgs e)
    > {
    > base.OnPreRender(e);
    >
    > string scriptKey = "intoPopupMessage:" + this.UniqueID;
    >
    > if (!Page.IsStartupScriptRegistered(scriptKey) && this.Enabled &&
    > !Page.IsPostBack)
    > {
    > string scriptBlock =
    > @"<script language=""JavaScript"">
    > <!--
    > alert(""%%POPUP_MESSAGE%%"");
    > // -->
    > </script>";
    > scriptBlock = scriptBlock.Replace("%%POPUP_MESSAGE%%", this.PopupMessage);
    >
    > Page.RegisterStartupScript(scriptKey, scriptBlock);
    > }
    > }
    > }
    >
    > }
    >
    > Regards,
    > Manish
    > www.ComponentOne.com
    >
    > "Jonathan Wood" wrote:
    >
    >> Below is a user control (ascx file) I created.
    >>
    >> Everything works fine except, when I first place an instance of the
    >> control
    >> on a form and look at the associated properties, my initial property
    >> values
    >> appear blank. They only show a value if I enter a new value.
    >>
    >> What is the trick to have my default property values appear in the
    >> properties window?
    >>
    >> Thanks.
    >>
    >> --
    >> Jonathan Wood
    >> SoftCircuits Programming
    >> http://www.softcircuits.com
    >>
    >> <%@ Control Language="C#" ClassName="HelpButton" %>
    >>
    >> <script runat="server">
    >>
    >> protected string _helpFile = "Help.aspx";
    >> protected string _imageUrl = "Help.png";
    >>
    >> public string HelpFile
    >> {
    >> get { return _helpFile; }
    >> set { _helpFile = value; }
    >> }
    >>
    >> public string ImageURL
    >> {
    >> get { return _imageUrl; }
    >> set { _imageUrl = value; }
    >> }
    >>
    >> protected void Page_Load(object sender, EventArgs e)
    >> {
    >> RegisterClientScripts();
    >> }
    >>
    >> protected override void Render(HtmlTextWriter writer)
    >> {
    >> base.Render(writer);
    >>
    >> // Render control
    >> writer.Write(String.Format("<input type=\"image\" src=\"{0}\"
    >> alt=\"Help\"
    >> onclick=\"javascript:return ShowHelp('{1}');\"
    >> style=\"border-width:0px;\",
    >> />",
    >> ResolveUrl("~/Images/Help.jpg"), ResolveUrl("~/Help/" + HelpFile)));
    >> }
    >>
    >> protected void RegisterClientScripts()
    >> {
    >> // Create client-side scripts to generate kilograms from pounds and
    >> vice
    >> versa
    >> if (!Page.ClientScript.IsClientScriptBlockRegistered("ShowHelp"))
    >> {
    >> Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ShowHelp",
    >> "function ShowHelp(url){" +
    >> "window.open(url, \"_blank\",
    >> \"height=300,width=450,toolbar=no,status=no,menubar=no,location=no,scrollbars=yes\");"
    >> +
    >> "return false;}", true);
    >> }
    >> }
    >>
    >> </script>
    >>
    >>
     
    Jonathan Wood, Feb 8, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    471
    Guest
    Oct 1, 2006
  2. Max2006
    Replies:
    2
    Views:
    521
    Max2006
    Jul 13, 2007
  3. Default Values for Control Properties

    , May 3, 2006, in forum: ASP .Net Building Controls
    Replies:
    10
    Views:
    268
  4. Alexander Higgins
    Replies:
    3
    Views:
    1,093
    Teemu Keiski
    Jul 30, 2007
  5. Alexander Higgins
    Replies:
    0
    Views:
    457
    Alexander Higgins
    Jul 21, 2007
Loading...

Share This Page