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

J

Jonathan Wood

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>
 
M

Manish

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
 
J

Jonathan Wood

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 said:
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 said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top