0

We are localizing a webpart for Sharepoint 2013, and we will use a specific function to translate the terms (so we can switch from a database backed-up translation table to a resource and vice-versa).

For the html elements we are successfully using this, and it's working like a charm:

<td><%= MyNameSpace.MySubNameSpace.StaticClass.GetTranslation("My Text") %>:</td>

But for the asp controls the text is not working properly, although it compiles ok:

<asp:CheckBox ID="myCheckBox" runat="server" Checked="True" Text='<%# MyNameSpace.MySubNameSpace.StaticClass.GetTranslation("My Text") %>' />

The MyNameSpace.MySubNameSpace.StaticClass is static, as well as the GetTranslation function.

Any clue on this?

EDIT: There is a way I can write a function that loop all the asp controls, get their current text value and change it by passing it through my translation function? Something like this:

...
foreach(AspControl _control in WebPart.Controls)
{
    if (_control.typeof() == <Control with Text properties>)
        _control.Text = MyNameSpace.MySubNameSpace.StaticClass.GetTranslation(_control.Text);
}
...
  • did you try Text="..." ? – Nk SP Jan 14 '15 at 21:56
  • Nk SP, as I said, we are localizing the webpart, so fixing a text wouldn't do it. – tcbrazil Jan 15 '15 at 10:19
  • I meant using " instead of ' – Nk SP Jan 15 '15 at 12:24
  • Oh, now I understood. Yes, I tried, and as soon as I use double quotes, Visual Studio raises the error "The server tag is not well formed.". – tcbrazil Jan 15 '15 at 17:20
1

The <%# %> syntax in ASP.NET is only valid in a databinding context, such as in <ItemTemplate> containers of the ListView control, or on controls for which you explicitly call the DataBind() function.

Similarly frustrating, in this scenario, is that the <%= %> syntax corresponds to literal output, and cannot be used to assign values to control properties.

If you are committed to the course of using the <asp:CheckBox /> control, then I recommend setting the properties in a script block during the PreRender event. It would look something like this:

<script runat="server">
    protected override void OnPreRender(EventArgs e)
    {
        this.myCheckBox.Text = Namespace.Class.GetTranslation("My Text");
        base.OnPreRender(e);
    }
</script>

Note that if you have a code-behind class, you can omit the script tags and put the method in your code-behind.

Perhaps more elegant would be exploring whether the <asp:FormView /> meets your needs. It provides a container for a single databound object, and can support databinding syntax (<%# %>) within its various templates.

  • Thanks a lot kbrimington! I can't really change the controls itself, and I was trying to avoid the code-behind solution in exchange for a markup solution in order to keep the localization in the same file the visual elements are. But so far I understand I can't do that, I'll have to use the code-behind solution. – tcbrazil Jan 16 '15 at 17:00
  • 1
    The code-behind solution will work, and I certainly understand how desirable it would be to keep things in markup as much as possible. Take a look at learn-asp.net/asptutorials/formview.aspx, which introduces the FormView control. With this, you could bind your localization to the controls, so long as they are in the FormView's templates. The cost, though, is that you'll need to create a datasource to bind against. It's a little bit different of a development pattern than what I think you're doing, but may be worth exploring. Cheers! – kbrimington Jan 16 '15 at 19:57
  • Actually I came up with another solution: I'm looping through all the webparts controls, and I'm calling the translation function to change their text. It worked like a charm! PS: I had to put all the client html components that needed location as runat="server" in order to "see" them in the code-behind. But I avoided changing them to asp controls. – tcbrazil Jan 19 '15 at 16:53
0

You can use HTML tag instead of <asp:CheckBox

<input ID="myCheckBox" Checked="True" type="checkbox" />
<label><%# MyNameSpace.MySubNameSpace.StaticClass.GetTranslation("My Text") %></label>
  • Nk SP, if you read the question carefully, you'll see that for the HTML tags we already had it working. – tcbrazil Jan 16 '15 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.