localizing ax external template

  • Thread starter Thread starter Trapulo
  • Start date Start date
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
 
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.)
 
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
 
Back
Top