Web part not displaying Actual property value in Page

Manmath Kulkarni asked on November 24, 2016 16:37

Hi,

I am creating Web part with Title and Description with following properties.

Prpoerties: Title Description

HTML:

{% Title %}

{% Description %}

I am able to create Web part with properties Title and description. Also from visual studio I am able define layout in .ascx file for this web part as above.

When I add web part to Zone its showing {%Description%} text instead of actual value inserted while adding web part to zone.

Kindly provide help.

Best regards, Manmath

Correct Answer

Anton Grekhovodov answered on November 24, 2016 19:01

Hi Manmath,

Your webpart have to be inherited from CMSAbstractWebPart class.
So then you can use <%= ValidationHelper.GetString(GetValue("Title"), "") %> in ascx file (be aware that you need add some additional references) or you can define properties in webpart cs file

public string Title 
{
    get { return ValidationHelper.GetString(GetValue("Title"), "");
}

and then in ascx use just <%= Title %>

Or you can add some controls in ascx file like Literal (<asp:Literal runat="server" ID="ltTitle"/>) and then in cs file add the code to populate this control:

protected override void OnLoad(EventArgs e){
    if (StopProcessing) return;

    ltTitle.Text = Title; // or ValidationHelper.GetString(GetValue("Title"), "")
}
3 votesVote for this answer Unmark Correct answer

Recent Answers


Peter M answered on November 24, 2016 16:52 (last edited on November 24, 2016 16:52)

Your probably need to call macro resolver when you set your property

Public Property Title As String
    Get
        Return ValidationHelper.GetString(GetValue("Title"), "")
    End Get
    Set(value As String)
        SetValue("Title", MacroContext.CurrentResolver.ResolveMacros(value))
    End Set
End Property
0 votesVote for this answer Mark as a Correct answer

Manmath Kulkarni answered on November 24, 2016 17:11

Hi Peter,

Thanks for reply.

I have just tried this but its still not working.

Best regards, Manmath.

0 votesVote for this answer Mark as a Correct answer

Manmath Kulkarni answered on November 25, 2016 10:07

Hi Anton,

Thanks a lot for your reply. Its working.
I have two queries.

  1. In both cases do I need to add set value as well using this?

    set{ SetValue("Title", MacroContext.CurrentResolver.ResolveMacros(value));}

  2. When I use

    <%= ValidationHelper.GetString(GetValue("Title"), "") %>
    

    or

      <%= Title  %>
    

It works but in CMS Administration -> Pages application - MyPage -> Design Tab -> MyWebPart it shows following error. [Error loading the WebPart 'MyWebPart' of type 'MyWebPart']. If I remove above line, it does not show this error.

Thanks in advance.

Best Regards, Manmath.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on November 25, 2016 11:16

Manmath,

1) If you only read values from your webpart settings, it's not necessary
2) Yes, sometimes this solution doesn't work in Kentico administration interface, it depends on how other webparts are loaded on the page. That's why I sent you two solutions with inline C# code in ascx and with Literal control. You can read some articles about it:
The Controls collection cannot be modified because the control contains code blocks
The Controls collection cannot be modified... error

1 votesVote for this answer Mark as a Correct answer

Manmath Kulkarni answered on November 25, 2016 11:20

Hi Anton,

Its really helpful. Thanks a lot for your reply.

Regards, Manmath.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on November 25, 2016 12:08

Manmath,

You're welcome)

0 votesVote for this answer Mark as a Correct answer

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