在你的aspx/ascx的,你需要使用asp:Repeater
控制,這樣的事情:
<asp:Repeater runat="server">
<ItemTemplate>
<p>Date: <asp:Literal runat="server" ID="litDate"></asp:Literal></p>
<p>Description 1: <strong><asp:Literal runat="server" ID="litDesc1"></asp:Literal></strong></p>
<p>Description 2: <asp:Literal runat="server" ID="litDesc2"></asp:Literal></p>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
在代碼隱藏,你應該對象的集合綁定到該Repeater
,並處理OnDataBinding
事件,你應該分配適當的值到asp:Literal
控件:
class DataItem
{
public DateTime Date { get; set; }
public string Desc1 { get; set; }
public string Desc2 { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
rptData.DataSource = new[]
{
new DataItem { Date = new DateTime(2013, 9, 30), Desc1 = "Test Desc 1", Desc2 = "Test Desc 2" },
new DataItem { Date = new DateTime(2013, 9, 30), Desc1 = "Test Desc 3", Desc2 = "Test Desc 4" }
};
rptData.ItemDataBound += OnItemDataBind;
rptData.DataBind();
}
protected void OnItemDataBind(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var data = e.Item.DataItem as DataItem;
var dateLiteral = e.Item.FindControl("litDate") as Literal;
dateLiteral.Text = data.Date.ToString("yyyy-MM-dd");
var desc1Literal = e.Item.FindControl("litDesc1") as Literal;
desc1Literal.Text = data.Desc1;
var desc2Literal = e.Item.FindControl("litDesc2") as Literal;
desc2Literal.Text = data.Desc2;
}
}
顯示您迄今爲止所做的,然後我們可以從那裏幫助您。 – hallie 2014-09-30 08:48:02
在網格中使用模板字段並將UI用於UI – 2014-09-30 08:48:59
查看此處[需要將文本以大寫形式保存到數據庫](http://stackoverflow.com/questions/8421917/need-to-save-text-to-database大寫)和[使用LINQ to SQL插入數據到數據庫](http://geekswithblogs.net/dotNETvinz/archive/2010/03/11/inserting-data-to-database-using-linq-to-sql .aspx) – Izzy 2014-09-30 08:51:13