如何让我的中继结果显示逐行 [英] How do i get my repeater results to appear line by line
问题描述
<p runat="server" id="pServices">
<asp:Repeater runat="server" ID="rptServices" OnItemDataBound="rptServices_OnItemDataBound">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblService"></asp:Label>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label runat="server" ID="lblService"></asp:Label>
</AlternatingItemTemplate>
</asp:Repeater>
</p>
而在code后面我
And in the code behind I have
private void updatePageWithUserData(User UserProfile)
{
this.pProfile.InnerText = UserProfile.About;
rptServices.DataSource = UserProfile.Services;
rptServices.DataBind();
}
protected void rptServices_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Service serviceItem = (Service)e.Item.DataItem;
((Label)e.Item.FindControl("lblService")).Text = (serviceItem.ServiceName);
}
}
但结果我得到的是这样的:
But the result I get is like this:
软件开发Web开发
相反,我希望它显示为:
Instead I want it to appear as:
软件开发
Web开发
推荐答案
请记住,在WebForms的,所有的输出是HTML,和HTML作为一种语言并不关心空格或换行符。
Remember that in WebForms, all of your output is HTML, and HTML as a language does not care about spaces or line breaks.
所以,如果您在HTML查看源代码,你会看到这样的事情:
So, if you view source on that HTML you will see something like this:
<span>Software Development</span>
<span>Web Development</span>
现在,因为HTML不关心事实,那就是在文件中换行符,他们一起跑,你会看到:
Now, since HTML does not care about the fact that there is a line break in the file, they run together and you see:
软件开发Web开发
因此,为了得到空间,需要以指示的方式的HTML理解空间。有这样做的几种方法,但我会在这里建议三为你:
So, in order to get the space, you need to indicate that space in a way that HTML understands. There are a few ways of doing that, but I'll suggest three for you here:
-
您可以使用在&lt; BR /&GT;标签,这表明一个换行符应目测显示。如果你走这条路线,你的中继的项目模板应该是这样的:
You can use the <br/> tag, which indicates that a line break should be visually displayed. If you were to go this route, your repeater's item template would look like this:
<ItemTemplate>
<asp:Label runat="server" ID="lblService"></asp:Label><br/>
</ItemTemplate>
SPAN标签是HTML标签被称为一个内联元素,因为它并不关心行距。如果您使用DIV标签或P标签来包装您的标签中,这些标签是块元素,做尊重间距,让您的项目模板应该是这样的:
SPAN tags are an HTML tag called an inline element, because it does not care about line spacing. If you used a DIV tag or a P tag to wrap your label in, those tags are block elements and do respect spacing, so your item template would look like this:
<ItemTemplate>
<div><asp:Label runat="server" ID="lblService"></asp:Label></div>
</ItemTemplate>
您也可以像CSS块元素内联元素的行为。通过应用类名的标签,然后用CSS样式,你可以得到同样的结果。这将是这样的:
You can also make an inline element act like a block element with CSS. By applying a classname to the label and then using CSS to style it, you could get the same result. That would look like this:
<ItemTemplate>
<asp:Label runat="server" ID="lblService" CssClass="myStyle"></asp:Label>
</ItemTemplate>
然后CSS:
.mystyle { display: block; }
这是给你一些不同的方法来解决问题,并有助于解释为什么你的code不能正常工作。
That gives you a few different ways to solve the problem and helps explain why your code wasn't working.
这篇关于如何让我的中继结果显示逐行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!