Thank you Brenden, I am now trying to use the OnContentLoaded method. My results are still the same. Neither the default value in the cs file, nor the default value set in kentico webpart properties seem to be doing anything. The button shows blank, unless the value is set on an instance of the webpart. I am going to post a link of this forum to kentico support, so for the sake of that.
My original code, coming from the guide at https://docs.kentico.com/display/K8/Creating+new+web+parts :
HelloWorld.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/CMSWebParts/MyWebParts/HelloWorld.ascx.cs" Inherits="CMSWebParts_MyWebParts_HelloWorld" %>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
HelloWolrd.ascx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.PortalControls;
using CMS.Helpers;
public partial class CMSWebParts_MyWebParts_HelloWorld : CMSAbstractWebPart
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Text = ValidationHelper.GetString(this.GetValue("ButtonText"), "Show Time");
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
Label1.Visible = true;
}
}
And my attempts at trying to create this using properties and the OnContentLoaded method (named helloWord2):
helloWorld2.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/CMSWebParts/MyWebParts/helloWorld2.ascx.cs" Inherits="CMSWebParts_MyWebParts_helloWorld2" %>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
hellowWorld2.ascx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.PortalControls;
using CMS.Helpers;
public partial class CMSWebParts_MyWebParts_helloWorld2 : CMSAbstractWebPart
{
#region "public properties"
public string ButtonText
{
get
{
return ValidationHelper.GetString(this.GetValue("ButtonText"), "Show Time");
}
set
{
this.SetValue("ButtonText", "Show Time");
}
}
#endregion
#region "methods"
/// <summary>
/// Content loaded event handler.
/// </summary>
public override void OnContentLoaded()
{
base.OnContentLoaded();
SetupControl();
}
/// <summary>
/// Initializes the control properties.
/// </summary>
protected void SetupControl()
{
if (this.StopProcessing)
{
// Do not process
}
else
{
Button1.Text = ButtonText;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
Label1.Visible = true;
}
/// <summary>
/// Reloads the control data.
/// </summary>
public override void ReloadData()
{
base.ReloadData();
SetupControl();
}
#endregion
}
I think all of the default values are being overridden. The default text is set 3 times. Once on the ascx file, once on the ascx.cs file, and once in the CMS administration --> webparts app --> properties --> General --> Default Value.