Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > WebParts DataBinding Not Evaluating View modes: 
User avatar
Member
Member
bryanallott - 3/6/2013 3:45:04 AM
   
WebParts DataBinding Not Evaluating
I have a custom webpart and in the markup (.ascx) I have the following databinding expression
<%# CurrentUser.UserID %>

However, this doesn't render any value (despite the webpart inheriting CMSAbstractWebPart > AbstractUserControl) with the property CurrentUser.

If I use a macro {% CurrentUser.UserID %}
in one of the properties of the webpart, I get the value rendered fine (but that would be from the macro helpers so I can mimic that through a script as well, eg:
<div runat="server">
<%= CurrentUser.UserID %>
</div>

To get the same value.

I would really prefer a databinding expression though: why would this not resolve?
(And without having to set values in the code behind)

Thanks

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 3/6/2013 4:18:30 AM
   
RE:WebParts DataBinding Not Evaluating
Hello,

When you are using macros in specific areas (page layouts, ascx files etc) you may need to resolve the Macro with the help of our API.

You may find the information in the guide below.

http://devnet.kentico.com/docs/devguide/index.html?macro_expressions_overview.htm

For your particular request I have tested this code myself, so please try it your ascx file.
<script runat="server">
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
lbl1.Text = CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros("UserID is: {%CurrentUser.UserID%} ");
}
</script>

<asp:Label ID="lbl1" runat="server" Text="Label"></asp:Label>

Please let me know if that resolves your issue.

Best regards,
Richard Sustek

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 3/6/2013 7:17:10 AM
   
RE:WebParts DataBinding Not Evaluating
You can also use this simply in your transformation without creating or overriding an event like so:
<%# CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros("{% YourMacro() %}") %>

User avatar
Member
Member
bryanallott - 3/7/2013 12:13:23 AM
   
RE:WebParts DataBinding Not Evaluating
Hey FroggEye,

While that may work in a transformation, it doesn't work in a custom webpart control, "as is", particularly the databinding expression <%# %>

What I was hoping to achieve was a level of consistency in the markup between transformations and webparts, so that code blocks like the one above, can be shared with webparts without any adjustment.

?

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 3/7/2013 7:11:34 AM
   
RE:WebParts DataBinding Not Evaluating
Understood. A simple Page.DataBind() would call your code block or change it to an evaluation expression <%= %>

User avatar
Member
Member
bryanallott - 3/7/2013 12:20:10 AM
   
RE:WebParts DataBinding Not Evaluating
Thanks, Richard

That partially answers my question (but also gives me other ideas, thanks :), but I also realise the question might be a bit vague.

Working with your example above though, how could I achieve the same functionality in my webpart as is available in transformations so that I could keep the markup "code" consistent between the two. For example, in a transformation, I may be able to use:
<%# HELPERFUNCTION() %>

where HELPERFUNCTION is one of the global helpers, but in my webpart, I am unable to use the same syntax, since
<%# HELPERFUNCTION() %>

always returns empty inside a webpart, but otherwise fine in the transformation

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 3/7/2013 3:29:34 AM
   
RE:WebParts DataBinding Not Evaluating
Hi Bryanallott,

Glad I was able to help.

As for the question - I'm not sure what could be a purpose of this. You may achieve the same consistance in both webpart and aspx templates. Unfortunately you cant use the same syntax but you have to use the API script which I posted in the previous post to have the macro resolved. Otherwise the macro will not be solved as you have experienced.

Best regards,
Richard Sustek

User avatar
Member
Member
bryanallott - 3/11/2013 4:43:39 AM
   
RE:WebParts DataBinding Not Evaluating
Hi Richard,
Another example of where this might be relevant is:

<cms:QueryRepeater ID=.. runat=.. TransformationName=.. QueryName=.. WhereCondition=.. />

Now if I want to make the WhereCondition dependant on the CurrentUser, for example, do something like...

WhereCondition=<%# string.Format("UserGUID = '{0}', CurrentUser.CurrentUser.UserGUID) #>

Then I can keep the syntax consistent across the markup.

I can set static conditions in the WhereCondition, like ="UserID = 5", but evaluating dynamic values inside the attribute require a little bit "more"...

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 3/12/2013 10:17:28 AM
   
RE:WebParts DataBinding Not Evaluating
Hi,

I'm not familiar with any way you could use consistent approaches in different parts of Kentico. Sometimes you would need to resolve the macros using the expression I have posted above.

Sorry for any inconviniences.

Hope that it wont stand if way of implementing the functions you want.

Best regards,
Richard Sustek

User avatar
Member
Member
bryanallott - 3/12/2013 11:29:27 AM
   
RE:WebParts DataBinding Not Evaluating
No worries. Thanks, Richard.

I was hoping for an "even shorter" way but as it is, the array of webparts and controls built into Kentico are already cutting down on a lot of dev time so it's all good!

Also good to know I didn't miss anything either ;)

Thanks for you help.

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 3/12/2013 11:55:17 AM
   
RE:WebParts DataBinding Not Evaluating
You are welcome:)

Let us know if you would have any other questions.

Kind regards,
Richard Sustek