Monday, April 29, 2013

How to bind repeater in Asp.net?


Introduction: In this post I will show you how to use Repeater control in Asp.net. Commonly we used repeater to show data.
Description:
 I have created a table:


Now open the Visual Studio>Go to File>New>Website. Add the Connectionstring in web.config file of website.
<configuration>
       <connectionStrings>
    <add name="connection" connectionString="Data Source=SYS-1F78031ED0A;Initial Catalog=TestBlog;Integrated Security=True"/>
       </connectionStrings>
       <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
</configuration>



After that add new web form to website. Now Insert a Repeater control from Toolbox>>Data Controls into .aspx page. Now take a template fields to bind value fields and customize the layout of the individual rows. See below:
<table style=" border:1px solid #df5015; width ="800px" cellpadding="0">
        <tr><td><asp:Label ID="Lblerror" runat="server" Text="Label"></asp:Label> </td></tr></table>
      
        <asp:Repeater ID="repcomment" runat="server">
                     <HeaderTemplate>  
        <table style=" border:1px solid #df5015; width ="800px" cellpadding="0">
<tr style="background-color:#e8d5f1; color:White">
<td colspan="2">
<b>Latests Comment</b>
</td>
</tr>
</HeaderTemplate>
        <ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
<tr>
<td>
Subject:
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Post_Subject") %>' Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Post_Comment") %>'/>
</td>
</tr>
<tr>
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px" >
<tr>
<td>User: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("Post_Username") %>'/></td>
<td>Posted Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("Date") %>'/></td>

  </tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>

</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>
        </asp:Repeater>

After this now go to .aspx.cs page and add the namespace to page.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Create a private function to bind Repeater.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        if (!IsPostBack)
        {
            Bindrepeatar();
        }
      

    }
    private void Bindrepeatar()
    {
        try
        {
            SqlCommand cmd = new SqlCommand("select * from Repetar", con);
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                repcomment.DataSource = ds;
                repcomment.DataBind();
            }
            else
            {
                repcomment.Visible = false;
                Lblerror.Text = "No Data Found";
               
            }
        }
        catch (Exception ex)
        {

        }
    }
Check the result.
   

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment