Showing posts with label Repeater. Show all posts
Showing posts with label Repeater. Show all posts

Monday, February 2, 2009

Display Data in Repeater..

Repeater control digunakan untuk display data items dalam bentuk repeating list iaitu data berulang-ulang.. Repeater menggunakan templates. Paling kurang pun, setiap Repeater mesti ada ItemTemplates, manakala templates yang lain ialah sekadar pilihan untuk customize the appearance of the data list tersebut..
  • ItemTemplate
  • AlternatingItemTemplate
  • SeparatorTemplate
  • HeaderTemplate
  • FooterTemplate

Coding at Client Side:

<--asp:Repeater ID="Repeater1" runat="server">
     <--HeaderTemplate>
          <--div><--/div>
     <--/HeaderTemplate>
     <--ItemTemplate>
          <--div>
               <--a href="Profile.aspx?name=<%#Eval("Associate_Name")%>">
                    <--%#DataBinder.Eval(Container.DataItem, "Associate_Name")%>
               <--/a>
          <--/div>
     <--/ItemTemplate>
     <--AlternatingItemTemplate>
          <--div>
               <--a href="Profile.aspx?name=<%#Eval("Associate_Name")%>">
                    <--%#DataBinder.Eval(Container.DataItem, "Associate_Name")%>
               <--/a>
          <--/div>
     <--/AlternatingItemTemplate>
     <--FooterTemplate>
          <--div><--/div>
     <--/FooterTemplate>
<--/asp:Repeater>


Coding at Server Side:

protected void Page_Load(object sender, EventArgs e)
{
     try
     {
          if(!IsPostBack)
          {
               DataSet ds = new DataSet();
               SqlConnection con = new SqlConnection();
               con.ConnectionString = @"Data Source=klcpc292\SQLEXPRESS;" + @"Initial Catalog=FacePage; Integrated Security=SSPI;";
               con.Open();

               string query = "select Associate_Name from Associate_Profile where Associate_Role='Buddy'";

               SqlDataAdapter adap = new SqlDataAdapter(query,con);
               adap.Fill(ds);
               Repeater1.DataSource = ds.Tables[0].DefaultView;
               Repeater1.DataBind();
          }
     }
     catch(Exception ex)
     {
          Response.Write(ex.Message);
     }
}