Thursday, 22 May 2014

Bound Repeater Control in ASP.Net

Repeater Control

A Repeater control is a lightweight control and faster to display data compared to a grid view and data grid.

 
<asp:Repeater ID="Repeater1" runat="server" >
                    <HeaderTemplate>
                        <table class="table table-striped table-bordered">
                            <tr>
                                <td><b>CustomerID</b></td>
                                <td><b>Name</b></td>
                                <td><b>Contact Name</b></td>
                                <td><b>Contact Title</b></td>
                                <td><b>Address</b></td>
                                <td><b>City</b></td>
                                <td><b>Phone</b></td>                               
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "CustomerID"%> 
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "CompanyName"%> 
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "ContactName"%> 
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "ContactTitle"%> 
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "Address"%> 
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "City"%> 
                            </td>
                            <td>
                                <%# DataBinder.Eval(Container.DataItem, "Phone"%> 
                            </td>                           
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table> 
                    </FooterTemplate>
                </asp:Repeater>

Web.config

<connectionStrings>
    <add name="ConnectionString" connectionString="Server=localhost;userid=root;password=;
      Database=Testdb
providerName="MySql.Data.MySqlClient"/>      
</connectionStrings>


C#

 MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings
            [
"ConnectionString"].ConnectionString);
 
protected void Page_Load(object sender, EventArgs e)
        {
            //Query 
            string query = @"SELECT CustomerID,CompanyName,ContactName,ContactTitle,
               Address,City,Phone,Email FROM customers"
;
            DataTable dt = new DataTable();
            try
            {
                MySqlDataAdapter da = new MySqlDataAdapter(query, conn);
                da.Fill(dt);
            }
            catch (MySqlException ex)
            {
                ShowMessage(ex.ToString());
            }
            //Populate Repeater control with data 
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }

void ShowMessage(string msg)
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation""<script
                         language='javascript'>alert('"
 + msg + "');</script>");
        }


No comments:

Post a Comment