Saturday, December 31, 2011

How to user Repeater in asp.net

Repeater control is a item Data bound control and is used to display small and Flexible repeated list of items.It is similar to data list control and doesn't provide built in structure.It provides set of templates to define required structure for presenting data.The Item Template used to display the records.The static method DataBinder.Eval is used to evaluate the data at run time using late binding.Here i will show you how to bind the data to Repeater with Item template.In this example i will bind the reports of teachers for everyday using repeater
Repeater.Aspx:

 
 <%#DataBinder.Eval(Container.DataItem,"Title")%>
 
Descrption: <%#DataBinder.Eval(Container.DataItem, "Description")%> Fom Date: <%#DataBinder.Eval(Container.DataItem, "fromdate")%> Todate: <%#DataBinder.Eval(Container.DataItem, "todate")%> Send To: <%#DataBinder.Eval(Container.DataItem, "teachername")%> (Teacher)On:<%#DataBinder.Eval(Container.DataItem, "date")%> To CC: Status: <%#DataBinder.Eval(Container.DataItem, "status")%>
The method onitemdatabound is get the data from database and bind to the repeater control.With out using this method we can get the data at the time of page loading
Code behind:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
 {
  Label lbl2;
  StringBuilder ccc = new StringBuilder();
  lbl2 = (Label)e.Item.FindControl("lblcc");
  string strd = "select t.teachername,ut.utype,ut.utypeid from teachers t,usertype ut,leaves l  where l.utype=ut.utypeid and l.teacherid=t.teacherid  and l.date='"+lbl.Text+"' and l.studentid='"+ddlchildrens.SelectedValue+"' and l.branchid='"+lblbranch.Text+"' and l.asid='"+lblasid.Text+"'";
  cmd = new SqlCommand(strd, Con);
  Con.Open();
  dr = cmd.ExecuteReader();
  while (dr.Read())
   {
    utypeid = dr["utypeid"].ToString();
    if (utypeid != "3")
       {
        tname = dr["teachername"].ToString();
        utype = dr["utype"].ToString();
        ccc.Append(tname + "(" + utype + "), ");
       }
    }
  dr.Close();
  Con.Close();
  lbl2.Text = ccc.ToString();
 }
Post a Comment

Bel