Thursday 7 February 2013

Repeater Controls in ASP.NET

Repeater Controls in ASP.NET


The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items. 

Repeater is a Data Bind Control. Data Bind Controls are container controls. Data Binding is the process of creating a link between the data source and the presentation UI to display the data. ASP .Net provides rich and wide variety of controls, which can be bound to the data. 


Repeater has 5 inline template to format it:
1. <HeaderTemplate>
2. <FooterTemplate>
3. <ItemTemplate> 
4. <AlternatingItemTemplate>
5. <SeperatorTemplate>
6. <AlternatingItemTemplate> 

HeaderTemplate: This template is used for elements that you want to render once before your ItemTemplate section.

FooterTemplate: - This template is used for elements that you want to render once after your ItemTemplate section.

ItemTemplate: This template is used for elements that are rendered once per row of data. It is used to display records

AlternatingItemTemplate: This template is used for elements that are rendered every second row of data. This allows you to alternate background colors. It works on even number of records only.

SeperatorTemplate: It is used for elements to render between each row, such as line breaks.

Some point about Repeater Control
  • It is used to display backend result set. It is used to display multiple tuple.
  • It is an unformatted control. The Repeater control is a basic templated data-bound list. It has no built-in layout or styles, so you must explicitly declare all layout, formatting, and style tags within the control's templates.
  • The Repeater control is the only Web control that allows you to split markup tags across the templates. To create a table using templates, include the begin table tag (<table>) in the HeaderTemplate, a single table row tag (<tr>) in the ItemTemplate, and the end table tag (</table>) in the FooterTemplate.
  • The Repeater control has no built-in selection capabilities or editing support. You can use the ItemCommand event to process control events that are raised from the templates to the control.

We have to use scriptlets for data access.

Data Bind Control can display connected and disconnected model.

Data Bind Control have DataBind() method and DataBound event

-> DataBind()
-> ItemCreated -> Event
-> DataBound -> Event

System.Commom.Data        namespace
-> DBDataRecord               Class

Controls -> Child Control

Every DataBindControl implement collection.

The data in DataSource is bound to Repeater using its DataBind Method. Once the data is bound, the format of each data item is defined by a template like ItemTemplate.

Step by Step Repeater Control example in asp.net or Display data in tabular format asp net c#

Step by Step Repeater Control example in asp.net or Display data in tabular format asp net c#


Database



 Default.aspx


<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:TemplateField>
        <HeaderTemplate>Id</HeaderTemplate>
        <ItemTemplate>
        <asp:LinkButton runat="server" PostBackUrl='<%#"Default2.aspx?id="+Eval("id") %>'>Id</asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
        <HeaderTemplate>Product</HeaderTemplate>
        <ItemTemplate>
        <asp:Label runat=server Text='<%#Eval("pname") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
        <HeaderTemplate>Image</HeaderTemplate>
        <ItemTemplate>
        <asp:Image runat="server" ImageUrl='<%#"img/"+Eval("pimg") %>' Height="100px" Width="100px" />
        </ItemTemplate>
        </asp:TemplateField>
       
        </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>


 Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"Data Source=A8-2\ATMIYA;Initial Catalog=test;Integrated Security=True");
        con.Open();
        da = new SqlDataAdapter("select * from prd_mst",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


    }
}




Default2.aspx

<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
       
        <table border="1">
              
        </HeaderTemplate>
        <ItemTemplate>
       
        <tr>
            <td><%#Eval("id") %></td>
            <td rowspan="2"><asp:Image runat="server" ImageUrl='<%#"img/"+Eval("pimg") %>' Height="75px" Width="75px" /></td>
     </tr>
     <tr>
            <td><%#Eval("pname") %></td>
    </tr>
    <tr>
         <td colspan="2"><%#Eval("pdesc") %></td>
    </tr>
       
       
        </ItemTemplate>
        <FooterTemplate>
        </table>
        </FooterTemplate>
       
        </asp:Repeater>
   
    </div>
    </form>
</body>

Default2.aspx.cs

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"Data Source=A8-2\ATMIYA;Initial Catalog=test;Integrated Security=True");
        con.Open();
        da = new SqlDataAdapter("select * from prd_mst where id="+Request.QueryString["id"],con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Repeater1.DataSource = ds;
        Repeater1.DataBind();

    }
}



Tuesday 5 February 2013

What Is Templatefields In A Gridview Control Asp.Net C#



We use TemplateFields when we wish to display ASP.Net controls in a GridView column. We display ASP.Net controls in a GridView column to provide additional functionality in the user interface. For example, by placing a DropDownList control in a GridView column, users will be able to select a list of options from within the Gridview control interface. Other examples of providing more functionality to the GridView interface are placing Checkboxes, Labels, Textboxes and Validation controls. A Template field supports many types of templates and a list of template types is given in the table below.


TemplateType Description
AlternatingItemTemplate The contents of this template are displayed for every other row rendered by the GridView
EditItemTemplate The contents of this template are displayed when a row is selected for editing
FooterTemplate The contents of this template are displayed in the column footer
HeaderTemplate The contents of this template are displayed in the column header
InsertTemplate The contents of this template are displayed when a new data item is inserted
ItemTemplate The contents of this template are displayed for every row rendered by the GridView

We can create TemplateFields in the GridView control using <TemplateField> element.

Steps to create the <TemplateField> element in the GridView control

a. Declare the GridView and set the AutoGenerateColumns property to ‘false’.
b. Create a Template column using <asp:TemplateField> tag within the <Columns> element.
Create within the <asp:TemplateField> element to display value of field as text.

Friday 1 February 2013

Step By Step. QueryString ,LinkButton , Image, Paging in GridView asp.net c#

Step By Step. QueryString ,LinkButton , Image, Paging in GridView asp.net c#

Default.aspx

<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            AllowPaging="true"
            onpageindexchanging="GridView1_PageIndexChanging" PageSize="4">
     
            <PagerSettings FirstPageText="First" LastPageText="Last" />
     
        <Columns>
        <asp:TemplateField>
       
        <HeaderTemplate>Id</HeaderTemplate>
      
       <ItemTemplate>
       <asp:LinkButton runat="server" ID="id" PostBackUrl='<%#"Default2.aspx?id="+Eval("id") %>'>Id</asp:LinkButton>
       </ItemTemplate>
       </asp:TemplateField>
      
       <asp:TemplateField>
       <HeaderTemplate>Image</HeaderTemplate>
       <ItemTemplate>
        <asp:Image runat=server ImageUrl='<%#"img/"+Eval("image") %>' Width="100px" Height="100px" />
      
       </ItemTemplate>
       </asp:TemplateField>
      
       <asp:TemplateField>
       <HeaderTemplate>Name</HeaderTemplate>
       <ItemTemplate>
        <asp:Label runat="server" ID=name><%#Eval("name") %></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>
        </Columns>
            <PagerStyle BackColor="#FF9933" BorderColor="Red" />
        </asp:GridView>
   
    </div>
    </form>
</body>






using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"Data Source=VSC3-121\SQLEXPRESS;Initial Catalog=TEST;Integrated Security=True");
        con.Open();
        databind();
           
    }
  

   public void databind()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from stud", con);
        DataSet ds = new DataSet();
        da.Fill(ds);

      
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        databind();
    }
}
Default2.aspx


<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>


Default2.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"Data Source=VSC3-121\SQLEXPRESS;Initial Catalog=TEST;Integrated Security=True");
        con.Open();
        databind();
           
    }
  

    public void databind()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from stud", con);
        DataSet ds = new DataSet();
        da.Fill(ds);

      
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        databind();
    }
}