Tuesday, December 16, 2014

Repeater Control example in asp.net

Introduction:

In this article I will explain what is repeater control, uses of repeater control and how to bind data to repeater control in asp.net.

Description:
 
In previous posts I explained many articles regarding Gridview, Ajax, JQuery and many more. Now I will explain about what is repeater control, uses of repeater control, bind data to repeater control in asp.net.
What is Repeater Control?
Repeater Control is a control which is used to display the repeated list of items
Uses of Repeater Control
Repeater Control is used to display repeated list of items that are bound to the control and it’s same as gridview and datagridview. Repeater control is lightweight and faster to display data when compared with gridview and datagrid. By using this control we can display data in custom format but it’s not possible in gridview or datagridview and it doesn’t support for paging and sorting. 
The Repeater control works by looping through the records in your data source and then repeating the rendering of it’s templates called item template. Repeater control contains different types of template fields those are
1) itemTemplate 2) AlternatingitemTemplate 3) HeaderTemplate 4) FooterTemplate
5) SeperatorTemplate
ItemTemplate: ItemTemplate defines how the each item is rendered from data source collection.



AlternatingItemTemplate: AlternatingItemTemplates is used to change the background color and styles of AlternatingItems in DataSource collection

HeaderTemplate: HeaderTemplate is used to display Header text for DataSource collection and apply different styles for header text.

FooterTemplate: FooterTemplate is used to display footer element for DataSource collection

SeparatorTemplate: SeparatorTemplate will determine separator element which separates each Item in Item collection. Usually, SeparateTemplate will be
html element or html element.


To implement repeater control sample first design table in your database as shown below

Table Name: tblCountry
Column Name
Data Type
Allow Nulls
CountryId
int(set identity property=true)
No
CountryName
varchar(50)
Yes

Table Name: tblState
Column Name
Data Type
Allow Nulls
StateId
int(set identity property=true)
No
CountryId
int

Statename
nvarchar(MAX)
Yes


public partial class Nestedrepeater : System.Web.UI.Page
{
    //Class_Main cm = new Class_Main();
  
    db_main asm = new db_main();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DisplayCountryData();
            DisplayStateData();
        }
    }
    public void DisplayCountryData()
    {
        DataSet ds = asm.fnGetSelect("Select * from tblcountry");
        rptcountry.DataSource = ds;
        rptcountry.DataBind();
    }
    public void DisplayStateData()
    {
        for (int i = 0; i <= rptcountry.Items.Count - 1; i++)
        {
            Label id = (Label)rptcountry.Items[i].FindControl("lblcountryid");
            Repeater rptState = (Repeater)rptcountry.Items[i].FindControl("rptstate");
            DataSet ds = asm.fnGetSelect("Select * from tblState where countryid=" + id.Text + "");
            rptState.DataSource = ds;
            rptState.DataBind();
        }
    }

    protected void rptcountry_ItemCommand(object source, RepeaterCommandEventArgs e)

    {
        if (e.CommandName == "edit")
        {
            Repeater rptstate = (Repeater)e.Item.FindControl("rptstate");
            ((Label)e.Item.FindControl("lblcountryname")).Visible = false;
            ((TextBox)e.Item.FindControl("txtcountryname")).Visible = true;
            for (int i = 0; i <= rptstate.Items.Count - 1; i++)
            {
                ((Label)rptstate.Items[i].FindControl("lblstatename")).Visible = false;
                ((TextBox)rptstate.Items[i].FindControl("txtstatename")).Visible = true;
            }
            ((LinkButton)e.Item.FindControl("lnkEdit")).Visible = false;
            ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = true;

        }
        else if (e.CommandName == "update")
        {
            Repeater rptstate = (Repeater)e.Item.FindControl("rptstate");
            for (int i = 0; i <= rptstate.Items.Count - 1; i++)
            {
                Label lblStateid = (Label)rptstate.Items[i].FindControl("lblstateid");
                TextBox txtstatename = (TextBox)rptstate.Items[i].FindControl("txtstatename");
             //  cm.select("update state set statename='" + txtstatename.Text + "' where stateid=" + lblStateid.Text + "");
            }
            TextBox txtcountryname = (TextBox)e.Item.FindControl("txtcountryname");
            //cm.select("update country set countryname='" + txtcountryname.Text + "' where countryid=" + e.CommandArgument + "");
            DisplayCountryData();
            DisplayStateData();
        }
        else if (e.CommandName == "cancel")
        {
            Repeater rptstate = (Repeater)e.Item.FindControl("rptstate");
            ((Label)e.Item.FindControl("lblcountryname")).Visible = true;
            ((TextBox)e.Item.FindControl("txtcountryname")).Visible = false;
            for (int i = 0; i <= rptstate.Items.Count - 1; i++)
            {
                ((Label)rptstate.Items[i].FindControl("lblstatename")).Visible = true;
                ((TextBox)rptstate.Items[i].FindControl("txtstatename")).Visible = false;
            }
            ((LinkButton)e.Item.FindControl("lnkEdit")).Visible = true;
            ((LinkButton)e.Item.FindControl("lnkUpdate")).Visible = false;
            ((LinkButton)e.Item.FindControl("lnkCancel")).Visible = false;
        }

    }
}


Download link

 

No comments:

Post a Comment