localizing ax external template

T

Trapulo

I've a datarepeater that loads custom external templates with loadtemplate
and bind data to them. All ok.
Now I need to localize some text labels, but I don't know how can I change
this data. If I call from template a function in page's codebehind, I have
an error. How can I do?

I wouldn't like create a lot of template, localized in asp code, and load
manually the right one..

thanks
 
S

Steven Cheng[MSFT]

Hi Trapulo,


Thanks for posting in the community!
From your description, you are using the ASP.NET Repeater server control to
display some set of datas and you want to localize the displayed data( can
I understand it as do some modification on them?) before they're displayed
and output to client, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, generally we have two approachs to accomplish such
operation:
1. Define a helper function in the code behind and call the helper function
in the databinding expresssion, for example, define a fucntion as below:
protected string ChangeText(string oldtext)
{
string new text = "new" + oldtext;
return newtext;
}

then in the page's aspx source, apply the below style databind expresssion:
<%# ChangeText(DataBinder.Eval(Container.DataItem,"fieldname")) %>

# one thing is important we must declare the function in code behind as
public or protected rather than private so that it is allowed to be called
in aspx page source.

2. Using the "ItemDataBound" event of the Repeater control, we can retrieve
the DataItem which will be binded to repeater item and the controls is to
be binded in the "ItemDataBound" Event, and do some modifications on them,
for example:
----------------------------
private void rptBound_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
Label lblIndex = (Label)e.Item.FindControl("lblIndex");
lblIndex.Text = GetLocalizedValue(drv["index"]);
}
}

For more detailed info on the "ItemDataBind" event, you may view the
following reference in the MSDN:
#Repeater.ItemDataBound Event
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuiwebcontro
lsrepeaterclassitemdataboundtopic.asp?frame=true


In addtion, to make the above descriptions clearly, I've made a sample page
using both the above two means, here is the page code:
----------------------------aspx page -------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataRepeater</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table align="center" width="500">
<TBODY>
<tr>
<td>Using Helper Method:<br>
<asp:Repeater id="rptHelper" runat="server">
<HeaderTemplate>
<table width="100%" align="center" border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#
GetLocalizedValue(DataBinder.Eval(Container.DataItem,"index")) %></td>
<td><%#
GetLocalizedValue(DataBinder.Eval(Container.DataItem,"name")) %></td>
<td><%#
GetLocalizedValue(DataBinder.Eval(Container.DataItem,"description")) %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate> </asp:Repeater></TD></TR>
<tr>
<td>Using ItemDataBound Event:<br>
<asp:Repeater id="rptBound" runat="server">
<HeaderTemplate>
<table width="100%" align="center" border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblIndex" Runat=server Text='<%#
DataBinder.Eval(Container.DataItem,"index") %>'>
</asp:Label></td>
<td>
<asp:Label ID="lblName" Runat=server Text='<%#
DataBinder.Eval(Container.DataItem,"name") %>'>
</asp:Label></td>
<td>
<asp:Label ID="lblDescription" Runat=server Text='<%#
DataBinder.Eval(Container.DataItem,"description") %>'>
</asp:Label></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</TBODY></TABLE>
</form>
</body>
</HTML>

-----------------------code behind page class-----------------
public class DataRepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater rptHelper;
protected System.Web.UI.WebControls.Repeater rptBound;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
LoadData();
BindRepeater();
}
}

protected void LoadData()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("description");

for(int i=0;i<15;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();

newrow["index"] = index.ToString();
newrow["name"] = "Name" + index.ToString();
newrow["description"] = "Description" + index.ToString();

tb.Rows.Add(newrow);
}


Session["TEMP_DATA"] = tb;

}

protected void BindRepeater()
{
DataTable tb = (DataTable)Session["TEMP_DATA"];

rptHelper.DataSource = tb;
rptHelper.DataBind();

rptBound.DataSource = tb;
rptBound.DataBind();
}


protected string GetLocalizedValue(object obj)
{
string lvalue = obj.ToString() + "_After_Localize";
return lvalue;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.rptBound.ItemDataBound += new
System.Web.UI.WebControls.RepeaterItemEventHandler(this.rptBound_ItemDataBou
nd);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void rptBound_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
Label lblIndex = (Label)e.Item.FindControl("lblIndex");
Label lblName = (Label)e.Item.FindControl("lblName");
Label lblDescription = (Label)e.Item.FindControl("lblDescription");
lblIndex.Text = GetLocalizedValue(drv["index"]);
lblName.Text = GetLocalizedValue(drv["name"]);
lblDescription.Text = GetLocalizedValue(drv["description"]);
}
}
}
------------------------------------------------------------

Please check out the preceding suggestions and sample. If you have any
questions or anything unclear on them, please feel free to post here.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
T

Trapulo

Steven Cheng said:
Hi Trapulo,


Thanks for posting in the community!
From your description, you are using the ASP.NET Repeater server control to
display some set of datas and you want to localize the displayed data( can
I understand it as do some modification on them?) before they're displayed
and output to client, yes?
If there is anything I misunderstood, please feel free to let me know.

some of this, but not exact this.
My localization is not about data-retrived text, but about itemtemplate.

E.g.
Label 1 <%# .... bind label 1... %>
Label 2 <%# .... bind label 2... %>

My problem is to localize Label 2 but not create a lot of templates (because
if I have a template for each language I neet to update all every time I
need some change)
As for this question, generally we have two approachs to accomplish such
operation:
1. Define a helper function in the code behind and call the helper function
in the databinding expresssion, for example, define a fucntion as below:
protected string ChangeText(string oldtext)
{
string new text = "new" + oldtext;
return newtext;
}

then in the page's aspx source, apply the below style databind expresssion:
<%# ChangeText(DataBinder.Eval(Container.DataItem,"fieldname")) %>

# one thing is important we must declare the function in code behind as
public or protected rather than private so that it is allowed to be called
in aspx page source.

This is the same way I've tried, and also the solution I user for similar
problem when template is inside main page file and not an external file
(BTW, I think for my problem this is a bad solution because for each row I
call this function, and I'm localizing label and not data, but I've not
better solution).
The problem is that I have an error compiling the page:
Name 'ChangeText' is not declared.
I think because the function is in codebehing of the page, but template is
on other file and loaded with Page.LoadTemplate function. On some situations
I load differente templates with:
fileList2.HeaderTemplate =
Page.LoadTemplate("templates/searchResultsHeader.ascx")

fileList2.ItemTemplate =
Page.LoadTemplate("templates/searchResultsItem.ascx")

(please note files have ascx extension, but the are only aspx code as inside
itemtemplate tag)
2. Using the "ItemDataBound" event of the Repeater control, we can retrieve
the DataItem which will be binded to repeater item and the controls is to
be binded in the "ItemDataBound" Event, and do some modifications on them,
for example:
----------------------------
private void rptBound_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
Label lblIndex = (Label)e.Item.FindControl("lblIndex");
lblIndex.Text = GetLocalizedValue(drv["index"]);
}
}

This can be a solution, but I cannot run it.

I addedd this code into the template:
<asp:Label ID="lblSearchResultTitle" Runat=server ></asp:Label>
And in event handler:
If e.Item.ItemType = ListItemType.Header Then

DirectCast(e.Item.FindControl("lblSearchResultTitle"), Label).Text = "Got
it!!"

End If


But the findcontrol returns nothing :(

In addtion, to make the above descriptions clearly, I've made a sample page
using both the above two means, here is the page code:

Thank for your work! However what you made is what I made. This works, but
only if template and datarepeater are into the same file. If template is in
external file, it seems this solutions don't work.

thanks
 
T

Trapulo

Steven Cheng said:
Private Sub rptMain_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
rptMain.ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
'Get the template control first since all the
'other controls(binded with data) are its child control
Dim template As Control = e.Item.Controls(0)

'Then retrieve other controls from the template's controls
collection
Dim lblIndex As Label = template.FindControl("lblIndex")

lblIndex.Text = Translate(lblIndex.Text)
End If
End Sub

Do you think so?

Great, that works!
Thanks, I appreciate ayour assistance: I resolved my problem and now my
application is working.


One last question: is there any way to localize labels in templates without
to repeat this operation for every item in datasource? Labels that depends
from language localization but not from single row-data content, I mean.

eg:
Name <asp:label.......><br>
Surname <asp:label...><br>

I think is better, if I can, to transale once for databinding process "Name"
ans "suranme", and not inside databound event handler for every row...
However this is only a performance optimization..
 
Joined
Aug 4, 2006
Messages
3
Reaction score
0
Follow-up

We would like to use the first suggestion, but can not seem to do it when the control is created using loadtemplate.

We have a datarepeater and load the templates (header, item, footer) dynamically with Page.LoadTemplate. In one of the item templates we were using a function call to the code behind i.e.

<td align="right"><%# CheckNull(DataBinder.Eval(Container.DataItem, "columnName")) %></td>

This worked when we defined our repeater and templates as part of the .aspx file and did not load them dynamically.

However this function call does not work when using the LoadTemplate. We placed the CheckNull function in the page code behind and then in the control code behind. We also tried variations of Public & Proctected. Each time we errored out on the LoadTemplate call.

The wierd part is we can use the system items like format, i.e.

<td align="left"><%# Format(DataBinder.Eval(Container.DataItem, "event_time"), "M/d h:mm:ss tt") %></td>

How does it find the Format function, but not our CheckNull function?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top