我如何做一个ASP.NET DataRepeater控件中的条件逻辑? [英] How do I do conditional logic within an ASP.NET DataRepeater control?
问题描述
我结合我DataRepeater控件到具有很多列的表。我想只有那些显示的一个子集,这取决于填充的。
I'm binding my DataRepeater control to a table that has many columns. I'd like to only display a subset of those, depending on what is populated.
如何/我应该在哪里做我一个的DataRepeater内contitional测试?这是我的ItemTemplate内的code:
How/where should I do my contitional tests within a dataRepeater? This is the code within my itemtemplate:
<% if (0= (DataBinder.Eval(Container.DataItem, "first").ToString().Length))
{
i++;
}
%>
我得到的错误是:CS0103:'集装箱'这个名字并不在目前的情况下存在
The error I get is: CS0103: The name 'Container' does not exist in the current context
推荐答案
您应该罚款与此:
<% if (0 == (Eval("first").ToString().Length))
{
i++;
}
%>
不过,这取决于你想做的事,我可能会写一个函数来处理数据的结合,以保持显示屏和业务逻辑之间的分离。
But depending on what you want to do, I would probably write a function to handle the binding of the data in order to retain separation between display and business logic.
例如
在你的aspx:
<asp:Repeater id="myRepeater" runat="server" onDataItemBound="FillInRepeater">
<ItemTemplate>
<div class="contactLarge">
<div style="background-color:#C5CED8;clear:both"><asp:Label runat="server" ID="title"></asp:Label>
.
.
.
</div>
</ItemTemplate>
<AlternatingItemTemplate>
</AlternatingItemTemplate>
</asp:Repeater>
在code-背后:
in your code-behind:
protected void FillInRepeater(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//in here you bind to your repeater labels and stuff then do all that sorta logic.
//Grab Primary Data
string titleText = DataBinder.Eval(e.Item.DataItem, "title").ToString();
string somethingElseText = DataBinder.Eval(e.Item.DataItem, "somethingElse").ToString();
string maybeSeeMaybeDontText = DataBinder.Eval(e.Item.DataItem, "maybeSeeMaybeDont").ToString();
//Find the controls and populate them according the to row
Label titleLabel = (Label)e.Item.FindControl("title");
Label somethingElseLabel = (Label)e.Item.FindControl("somethingElse");
Label maybeSeeMaybeDontLabel = (Label)e.Item.FindControl("maybeSeeMaybeDont");
// display the fields you want to
titleLabel.Text = titleText;
somethingElseLabel.Text = somethingElseText;
// here is where you could do some of your conditional logic
if (titleText.Length != 0 && somethingElseText.Length != 0)
{
maybeSeeMaybeDontLabel.Text = maybeSeeMaybeDontText;
}
}
}
我个人preFER做事这种方式,而不是在做ASP内的任何逻辑。我知道,这似乎有点傻一些人,但我想保持我的业务逻辑分离从我的显示逻辑等。无论可能的。
personally, I prefer to do things this way rather than doing any logic inside the ASP. I know that it might seem a bit silly to some people, but I like to keep my business logic separate from my display logic whereever possible.
这篇关于我如何做一个ASP.NET DataRepeater控件中的条件逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!