Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday 18 February 2015

In this article i will explain how to Show empty template in Repeater with "No records available to display" message when there is no data available in table.




In this Article I will explain the following things:
1.       Bind the data from sql server database table to repeater data control.
2.       How to display message in repeater with just header id data is not there.
3.       How to find HTML control inside Repeater's Footer.


In this example we will find the div placed inside Repeater and set its "visible" property to make it visible.  


Create a DataBase “Test” in Sql server, now create a table “Dept_table” with the following Columns and Data type as shown below :  

Column Name
Data Type
Dept_Id_Pk
Int(Primary Key. So set is identity=true)
Dept_Name
varchar(100)


Now let's connect application with Sql Server database:
 <connectionStrings>
    <add name="EmpCon" connectionString="Data Source=localhost;Initial Catalog=test;IntegratedSecurity=True"/>
  </connectionStrings>


Designing Part of  the page :

<fieldset style="width:200px; text-align:center;">
    <legend>Empty Repeater Example</legend>
    <table>
    <tr><td class="style1"></td>
    <td align="center">
      <asp:Repeater ID="rptDept" runat="server"
            onitemdatabound="rptDept_ItemDataBound1">
        <HeaderTemplate>
        <table border="1" cellpadding="1" width="200px;">
        <tr style="background-color:#1B3854; color:#FFF; height:30px;" align="center">
        <th>Department Name</th>
       
        </tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr style="background-color:white;" align="center">
        <td><%#Eval("Dept_Name") %></td>
       
        </tr>
        </ItemTemplate>    
     
       <FooterTemplate>
        </table>
        <div id="dvNoRecords" runat="server" visible="false" style="padding:20px 20px; text-align:center;color:Red;">
        No records to display.
        </div>
        </FooterTemplate>
        </asp:Repeater>
    </td>
    <td class="style2"></td>
    </tr>
           
    </table>
    </fieldset>
    </td>
    </tr>
    </table>
    </fieldset>

Asp.Net C# Section:
In code behind, write the code as;

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;
using System.Configuration;
using System.Web.UI.HtmlControls;
public partial class Empty_Repeater : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepeater();
        }
    }

    protected void BindRepeater()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString.ToString());
        SqlCommand cmd = new SqlCommand("Select * from Dept_Table where Dept_Name='XYZ'", con);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        rptDept.DataSource = dt;
        rptDept.DataBind();
        con.Close();
    }
 
protected void  rptDept_ItemDataBound1(object sender, RepeaterItemEventArgs e)
{
 if (rptDept.Items.Count < 1)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                HtmlGenericControl dvNoRec = e.Item.FindControl("dvNoRecords") as HtmlGenericControl;
                if (dvNoRec != null)
                {
                    dvNoRec.Visible = true;
                }
            }
        }
}
}


 Asp.Net VB Section:

Design the page as  in above Asp.net C#  section but replace the lines

<asp:Repeater ID="rptStudentDetails" runat="server"
            onitemdatabound=" rptDept_ItemDataBound">
with following line:
<asp:Repeater ID="rptDept" runat="server">
  
In the code behind file write the code as:

 Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.HtmlControls

Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("EmpCon").ConnectionString)

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindRepeater()
        End If
    End Sub

    Protected  Sub BindRepeater()
        Dim con As SqlConnection =  New SqlConnection(ConfigurationManager.ConnectionStrings("EmpCon").ConnectionString.ToString())
        Dim cmd As SqlCommand =  New SqlCommand("Select * from Dept_Table where Dept_Name='XYZ'",con)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        Dim dt As DataTable =  New DataTable()
        Dim adp As SqlDataAdapter =  New SqlDataAdapter(cmd)
        adp.Fill(dt)
        rptDept.DataSource = dt
        rptDept.DataBind()
        con.Close()
    End Sub

    Protected Sub rptDept_ItemDataBound(sender As Object, e AsSystem.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptStudentDetails.ItemDataBound
        If rptDept.Items.Count < 1 Then
            If e.Item.ItemType = ListItemType.Footer Then
                Dim dvNoRec As HtmlGenericControl = TryCast(e.Item.FindControl("dvNoRecords"),HtmlGenericControl)
                If dvNoRec IsNot Nothing Then
                    dvNoRec.Visible = True
                End If
            End If
        End If
    End Sub

End Class

0 comments:

Post a Comment