Follow by Email

Monday, June 24, 2013

bind data to Repeater in asp.net | Repeater Example

hi in this post i will demonstrate on how to bind data to a Repeater in asp.net.

Aspx code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" Visible="true" runat="server">
            <HeaderTemplate>
             <table border="1" width="30%">
                    <tr>
                        <th>
                            Name
                        </th>
                        <th>
                            Salary
                        </th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "Name")%>
                    </td>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "Salary")%>
                    </td>
                </tr>
            </ItemTemplate>
          
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Codebehind:

 public void BindRepeater()
    {

        string conStr = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString.ToString();
        SqlConnection s1 = new SqlConnection(conStr);
        s1.Open();

        string queryString = "select empname as Name,salary from employee";
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, s1);

        DataSet employee = new DataSet();
        adapter.Fill(employee, "employee");

        Repeater1.DataSource = employee.Tables[0];
        Repeater1.DataBind();

        s1.Close();

    }

Result :