2014-09-30 11 views
0

ASP.NET과 SQL을 사용하고 있습니다.ASP.NET에서 데이터 형식을 지정하는 방법

테이블에 3 개의 열이 있습니다. 나는 그런 방식으로 데이터를 채워야 :

Date: 2014-09-22 
Description1: xyz (this should be bold) 
Description2 pqrs (normal paragraph) 

을 수평 태그 다음에 다른 데이터가 나타납니다


Date: 2013-09-22 
Description1: abcd (this should be bold) 
Description2 qwe (normal paragraph) 

나는 GridView의 데이터를 채울 수 있어요하지만 나도 몰라 이 방식으로 데이터를 포맷하는 방법. 나는 ASP.NET을 처음 사용합니다.

몇 가지 도구 나 링크를 제안하거나 코드 작성을 도와주십시오. 당신 영문/ASCX에서

+0

당신이 지금까지해온 것을 보여준 다음 거기에서 도울 수 있습니다. – hallie

+0

격자의 템플릿 필드를 사용하고 UI에 html을 사용합니다. –

+0

여기를 보겠습니다. [대문자로 데이터베이스에 텍스트를 저장해야합니다.] (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

답변

0

당신이 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; 
    } 
} 
+1

감사합니다 ... 그 작품 ... 내가 예상대로! ...이 코드를 수정하십시오

날짜 :

+0

네,'DataBinder.Eval'도 잘 작동합니다. 필자는 속성 이름을'DataBinder.Eval'에서와 같이 문자열로 전달하는 것보다 강력한 형식을 선호합니다. – tdragon

관련 문제