repeater数据邦定后。我想对其中一列的数据进行格式化怎么办?
例如repeater显示如下数据:
a b c d 1
e f g h 2
h i j k 1
....
这是一张表存储的数据。但是我想对第5列的数据进行格式化。例如:1,我不是让其直接显示1,而是显示中国。2,是显示美国。但数据一绑定,就会直接显示1,2。现在该怎么办?
------解决方案--------------------//aspx:
<form id= "form1 " runat= "server ">
<div>
<asp:Repeater ID= "Repeater1 " runat= "server " OnItemDataBound= "Repeater1_ItemDataBound ">
<ItemTemplate>
<asp:Label ID= "Label1 " runat= "server " Text= ' <%# DataBinder.Eval(Container.DataItem, "au_id " ) %> '> </asp:Label>
<asp:Label ID= "Label2 " runat= "server " Text= ' <%# DataBinder.Eval(Container.DataItem, "state " ) %> '> </asp:Label>
<br />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
//aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection cn = new SqlConnection(@ "Server=.\SQLEXPress;uid=sa;pwd=password;Database=Pubs ");
string strSQL = "select * from authors ";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
Repeater1.DataSource = dr;
Repeater1.DataBind();
dr.Close();
cn.Close();
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label label2;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
label2 = (Label)e.Item.FindControl( "label2 ");
switch(label2.Text)
{
case "CA ":
label2.Text = "加州 ";
break;
default:
label2.Text = "XX ";
break;
}
}
}
------解决方案--------------------databind时,做选择了,若为1输出为中国。