来源:http://topic.csdn.net/u/20080611/08/aa9118ca-ea87-46a0-8e5b-36dbe2b154af.html
http://dotnet.ygman.com/thread/107934
repeater的数据绑定问题
需要绑顶2个字段
<%#Eval("name")%>
<%#Eval("h_name")%>
<%#Eval("h_oname")%>
根据业务需要。
绑定的时候 这2个绑定的字段如果 <%#Eval("name")%>等于"小明"的时候就显示 <%#Eval("h_oname")%> 如果 <%#Eval("name")%>等于"大明"的时候就显示 <%#Eval("h_name")%>
怎么写判断?
谢谢了
如果是vs2003 就用DataBinder.Eval( Container.DataItem, "name" )代替Eval("name")
试下
1.
<%#Eval("name").ToString()=="小明"?Eval("h_oname"):Eval("h_name")%>
2.
楼主没有说如果即不是小明也不是大明的时候怎么办,估计就是显示 <%#Eval("name")%>了
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%# Eval("name").ToString() == "小明" ? Eval("h_oname") : (Eval("name").ToString() == "大明" ? Eval("h_name") : Eval("name")) %>
</ItemTemplate>
</asp:Repeater>
3.
public string ShowName(object val,object name1,object name2)
{
//请确实val最终是个值类型,这样就能比较大小,如:
//在页面的设用方法 <%# ShowName(Eval("name"),Eval("h_name"),Eval("h_oname")%>
return (Convert.ToInt32(val) > 12345) ? name1.ToString() : name2.ToString();
}
4.
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<div><asp:Label runat="server" ID="lblName"></asp:Label></div>
</ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
Label l = e.Item.FindControl("lblName") as Label;
if ("小明".Equals(drv["name"].ToString()))
{
l.Text = drv["h_oname"].ToString();
}
else
{
l.Text = drv["h_name"].ToString();
}
}
}
1.在html里绑定数据,如果绑定的数据需要判断,如果有两个条件,可以使用三元运算符:
例如:A=="5" ? "B" : "C"
如果一个Lable绑定数据:<asp:label id="lblProjectName" runat="server" Text='<%# Eval("ID")%>'></asp:label>,如果需要判断可以这样写:
<asp:label id="lblProjectName" runat="server" Text='<%# Eval("ID").ToString() == "5" ? "B" : "C" %>'></asp:label>,
当条件多于两个时,可用嵌套运算:
<asp:label id="lblProjectName" runat="server" Text='<%# Eval("ID").ToString() == "5" ? "B" :(Eval("ID").ToString() == "6" ? "C" : "D") %>'></asp:label>,
依次类推。
2.写个public方法放在。cs中,然后在。aspx中数据绑定时调用
//相关Row数据处理
protected void GVDesignList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
Button btnDel = (Button)e.Row.FindControl("GVBtnDel");
if (btnDel != null)
{
btnDel.Attributes.Add("onclick", "return confirm('你确定要删除所选择的数据行吗?');");
}
//删除按钮
if (((Label)e.Row.Cells[60].FindControl("lblAdidName")).Text.Trim() == UserName)
{
((Button)e.Row.Cells[1].FindControl("GVBtnDel")).Enabled = true;
}
else
{
((Button)e.Row.Cells[1].FindControl("GVBtnDel")).Enabled = false;
}
//颜色
if (((Label)e.Row.Cells[60].FindControl("GVlblactionZK")).Text.Trim() =="已实施")
{
e.Row.Cells[2].ForeColor = System.Drawing.Color.Green;
e.Row.Cells[3].ForeColor = System.Drawing.Color.Green;
e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;
e.Row.Cells[5].ForeColor = System.Drawing.Color.Green;
}
else if (((Label)e.Row.Cells[60].FindControl("GVlblactionZK")).Text.Trim() == "未实施")
{
e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[5].ForeColor = System.Drawing.Color.Red;
}
else if (((Label)e.Row.Cells[60].FindControl("GVlblactionZK")).Text.Trim() == "需确认项目")
{
e.Row.Cells[2].ForeColor = System.Drawing.Color.BlueViolet;
e.Row.Cells[3].ForeColor = System.Drawing.Color.BlueViolet;
e.Row.Cells[4].ForeColor = System.Drawing.Color.BlueViolet;
e.Row.Cells[5].ForeColor = System.Drawing.Color.BlueViolet;
}
else if (((Label)e.Row.Cells[60].FindControl("GVlblactionZK")).Text.Trim() == "实施正确项目")
{
e.Row.Cells[2].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[3].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[4].ForeColor = System.Drawing.Color.Blue;
e.Row.Cells[5].ForeColor = System.Drawing.Color.Blue;
}
//ForeColor backColor
}
}