Saturday, July 28

How to Bind a repeater control in asp.net.


Repeater Control Source Code:-

<asp:Repeater ID="RepeaterLeaveStatus" runat="server">
                <ItemTemplate>
                    <asp:HiddenField ID="hdnLeaveTypeID" runat="server"Value='<%#Bind("Leave_Type_ID") %>' />
                    <table class="repeater" cellpadding="0" cellspacing="0">
                        <tr>
                            <td colspan="3" class="repeater_td_header">
                                <asp:Label ID="lblLeaveType" runat="server"Text='<%#Bind("Leave_Type_Name") %>'></asp:Label>
                            </td>
                        </tr>
                        <tr class="repeater_tr_content">
                            <td style="width: 50%; padding-left: 3%">
                                <b>Allocated</b>
                            </td>
                            <td style="width: 4%">
                                <b>:</b>
                            </td>
                            <td style="width: 40%; text-align: left;">
                                <asp:Label ID="lblAllocated" runat="server"Text='<%#Bind("Allocated_Days") %>'></asp:Label>
                            </td>
                        </tr>
                        <tr class="repeater_tr_content">
                            <td style="width: 50%; padding-left: 3%">
                                <b>Leaves Taken</b>
                            </td>
                            <td style="width: 4%">
                                <b>:</b>
                            </td>
                            <td style="width: 40%; text-align: left;">
                                <asp:Label ID="lblTaken" runat="server"Text='<%#Bind("No_Of_Days") %>'></asp:Label>
                            </td>
                        </tr>
                        <tr class="repeater_tr_content" style="background-color: #90EE90">
                            <td style="width: 50%; padding-left: 3%">
                                <b>Available</b>
                            </td>
                            <td style="width: 4%">
                                <b>:</b>
                            </td>
                            <td style="width: 40%; text-align: left;">
                                <asp:Label ID="lblAvailable" runat="server"Text='<%#Bind("Available_Days") %>'></asp:Label>
                                <asp:HiddenField ID="HdnAvailableDays"runat="server" Value='<%#Bind("Available_Days") %>' />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
                <%-- <SeparatorTemplate>
                    <hr />
                </SeparatorTemplate>--%>
            </asp:Repeater>
Design View:

Code Behind method to bind the Repeater:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillRepeater();
        }
    }

    public void FillRepeater()
    {
        SqlConnection con=new SqlConnection("...Your Connection String...");
        SqlCommand SelectDetails = new SqlCommand("select Leave_Type_ID,Leave_Type_Name,Allocated_Days,No_Of_Days,Available_Days From Employee_Leave_Details", con);
        DataTable dtRecords = new DataTable();
        SqlDataAdapter dap = new SqlDataAdapter(SelectDetails);
        dap.Fill(dtRecords);
        RepeaterLeaveStatus.DataSource = dtRecords;
        RepeaterLeaveStatus.DataBind();

    }
}

Output: 



Thanks

     Shibashish Mohanty

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty