在Asp.net段说我有
现在我想P中
显示在h1和内容标题带有属性的标题和内容的信息的数据我怎么使用for/while循环。使用C#Asp.net
产生这些标签,以便其像这样
标题和使用C#
标题1
内容
heading2
内容再次
,并不断去.. ?
在Asp.net段说我有
现在我想P中
显示在h1和内容标题带有属性的标题和内容的信息的数据我怎么使用for/while循环。使用C#Asp.net
产生这些标签,以便其像这样
标题和使用C#
内容
内容再次
,并不断去.. ?
我不确定是否理解,但是您的意思是说您获取某种数据,并且想要以这种方式显示?您应该使用ListView,在其中定义一个ItemTemplate,并将数据分配给DataSource属性。
例如,在你的.aspx文件
<asp:ListView ID="LvPost" runat="server" OnItemDataBound="LvPost_ItemDataBound">
<ItemTemplate>
<h1><asp:Literal id="title" runat="server"></h1>
<p><asp:Literal id="content" runat="server"></p>
</ItemTemplate>
</asp:ListView>
然后,在你LvPost_ItemDataBound方法类似
var postItem= e.Item.DataItem as YourPostClass;
var h1Text = e.Item.FindControl("title") as Literal;
var pText= e.Item.FindControl("content") as Literal;
h1Text.Text=postItem.Title;
pText.Text = postItem.Content;
最后,不要忘记将数据绑定到列表视图,(前:在PageLoad中)
LvPost.DataSource = <List of YourPostClass>;
我不确定你的数据到底如何,但是像这样可能会工作。把文字在网页上,你想要的数据出现,然后使用如下代码:
string[] headings = {"Heading 1", "Heading 2", "Heading 3"};
string[] paragraphs = {"Content", "content again","Content even again!"};
literal1.Text = "";
for (int i=0; i<headings.Length;i++)
{
literal1.Text = string.Format("{0}<h1>{1}</h1><p>{2}</p>{3}", literal1.Text, HttpServerUtility.HtmlEncode(headings[i]), HttpServerUtility.HtmlEncode(paragraphs[i]), Environment.Newline);
}
请注意,我使用HttpServerUtility.HtmlEncode
来编码字符串。如果你希望包括内容(例如,如果paragraphs[0] == "<b>Content</b>
“)内HTML标记,然后如果你喜欢一个List<T>
和容器类,而不是删除此方法
,该代码可能更合适:
private class Content
{
public string Heading { get; set; };
public string Paragraph { get; set; };
}
private List<Content> _content = new List<Content>();
private void CreateContent()
{
_content.Add(new Content {Heading = "Heading 1", Paragraph = "Content"});
_content.Add(new Content {Heading = "Heading 2", Paragraph = "More Content"});
_content.Add(new Content {Heading = "Heading 3", Paragraph = "Even More Content"});
literal1.Text = "";
foreach (Content c in _content)
{
literal1.Text = string.Format("{0}<h1>{1}</h1><p>{2}</p>{3}", literal1.Text, HttpServerUtility.HtmlEncode(c.Heading), HttpServerUtility.HtmlEncode(c.Paragraph), Environment.Newline);
}
}