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.)