Repeater绑定值如何按要求进行编辑
Repeater控件数据源SqlDataReader,现在数据库里分别有两个字段
list1,list2里面存放数据
0 0
1 0
2 1
.....
我想实现的目标是在list2为0时,则Lb1显示为"类别A";
list2为1时,要判断list1值,当list1值为0时,则Lb1显示为"类别BA",当list1值为1时,则Lb1显示为"类别BB",当list1值为2时,则Lb1显示为"类别BC",
想请教应该如何实现啊?
我在后台rpt2_ItemDataBound方法里不知道应该如何读取list2的值了?
还有下面的例子为何只有第一行有效果,其他的没有效果啊?
前台:
<asp:Repeater ID="rpt2" runat="server" onitemdatabound="rpt2_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="Lb1" runat="server" Text='<%#Eval("list1","{0}")%'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
后台:
protected void rpt2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
if (e.Item.ItemIndex !=-1)
{
Label lb = (Label)e.Item.FindControl("Lb1");
if (lb.Text == "1")
lb.Text = "(VIP会员)";
else if (lb.Text == "2")
lb.Text = "(认证会员)";
else
lb.Visible = false;
}
}
}
------解决方案--------------------
// SORRY ->
protected string FormatMyCategory(object dataItem)
{
// 假如绑定的是 DataTable/DataView, dataItem 的类型则为 DataRowView
System.Data.Common.DbDataRecord dr = dataItem as System.Data.Common.DbDataRecord;
string list1 = dr["list1"].ToString();
string list2 = dr["list2"].ToString();
if(list2 == "0") return "类别A"
else {
// if(list1 == "1") .....
}
}