hello world button text default not working

kyle shapiro asked on April 3, 2015 20:08

Hi, I did the tutorial for creating a hello world webpart in visual studio. It all works wonderfully, except one tiny issue. In my HellowWorld.ascx.cs file, I have the following in the page load event. Button1.Text = ValidationHelper.GetString(this.GetValue("ButtonText"), "Show time"); This sets the text property of Button1 to be equal to the ButtonText property on the webpart. This also works fantastic, however the part directly after it , "Show time" is supposed to be a default value for when no value is provided. I've checked on the webpart ButtonText property to make sure nothing was entered there, and I set no default value while defining that property... Why is it resolving to Text="" instead of the desired Text="Show time"? Peeking at the definition of ValidationHelper.GetString gives public static string GetString(object value, string defaultValue, CultureInfo culture = null); Should there be one more argument for this CultureInfo part? Have a good Good Friday!

Correct Answer

Brenden Kehren answered on April 7, 2015 14:41

My suggestion for default values is ONLY set them in code if you HAVE to. ALWAYS set them in the webpart definition in the UI.

So this code:

public string ButtonText
{
    get
    {
        return ValidationHelper.GetString(GetValue("ButtonText"), "Show Time");
    }
    set        
    {
        SetValue("ButtonText", value);
    }
}

Remove the "Show Time" in the getter and go to Webpart definition within Kentico and set the default value for the ButtonText property to "Show Time".

Technically, with your getter code, it should return "Show Time" if there is no value but if it is erroring out because that "ButtonText" property is not available, it will always return null. Make sure you have the webpart and property defined within Kentico.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Sean Wright answered on April 3, 2015 21:23

I would be interested to see if this.GetValue("ButtonText") was returning an empty string. I believe GetString will only use the default value if the first parameter is null or cannot be parsed into a string

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on April 3, 2015 22:00

Good point. maybe before this i could write an if statement returning the this.GetValue("ButtonText") as a string variable if it is not null or "", and as null otherwise. Then plug that variable in like so GetString(myStringVariable,"Show time"). I'll post Monday if this works. Either way, this is an example from kentico, and should work. Maybe the method GetString(); has changed since this example was created?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 4, 2015 15:43

Just a note about setting defaults in code, if you have the following code:

public string ButtonText
{
    get
    {
        return ValidationHelper.GetString(GetValue("ButtonText"), "Show Time");
    }
    set        
    {
        SetValue("ButtonText", value);
    }
}

If the user doesn't provide a value, it will always return "Show Time". BUT if you leave that default value empty and specify it within your webpart properties, you can change it whenever you want without having to modify code.

Another thing to note about webparts is in 99% of the cases you should set your webpart control values in the OnContentLoaded event and NOT the PageLoad event. Take a look an other webparts and you'll see the code you'll need to follow. Webparts have a different page lifecycle than a standard ascx control.

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on April 6, 2015 18:07

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.

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on April 6, 2015 18:54

I just checked to see if ValidationHelper.GetString(this.GetValue("ButtonText"), "Show Time") returned an empty string, and it does. So how can I point to the "Default Value" property of this "ButtonText" property I have created? I can place an if statement checking to see if ValidationHelper.GetString(this.GetValue("ButtonText"), "Show Time") returns "", and when it does, tell it to use that Default Value property of the BottonText property.

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on April 7, 2015 16:36

Hey, following up with this for future reference: Timothy Fenton, Support Engineer with Kentico was able to help me out. ValidationHelper.GetString() was a mistake in the documentation. It should be Button1.Text = DataHelper.GetNotEmpty(this.GetValue("ButtonText"), "Default Text");

Furthermore you can plug in the "default value" property of the "ButtonText" property of the webpart into the place where we have written "Default Text".

protectedvoid Page_Load(object sender, EventArgs e)
    {
        WebPartInfo test = WebPartInfoProvider.GetWebPartInfo(this.ID);
        Button1.Text = DataHelper.GetNotEmpty(this.GetValue("ButtonText"), test.GetWebPartFormInfo().GetFormField("ButtonText").DefaultValue);
    }

Thank you again Brenden, I will try your newest response when I get a chance. I understand that using properties (with get/set) is the norm for controls and webparts, and this will help me to learn that. Also, how do you paste code on these forums? My all-orange code is difficult to follow.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.