Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,
I want to display the number of records found in the datalist's header part.
Please any one suggest me, how to do that?
Posted
Comments
Karthik_Mahalingam 7-Jan-14 0:59am    
post your code..

add lable in the header template of datalist.

when you bind data to datalist use a method findcontrol of datalist control and find the lable by ID and create its object and assign value.



C#
lable lblCount = (lable) datalist.findcontrol("lblid");
if(lblCount !=null)
{
lblCount .text = yourdatatable.row.count.tostring();
}
 
Share this answer
 
Found in stacktrace...
Thanks..
protected void dlMembers_ItemDataBound(object sender, DataListItemEventArgs e)
   {
       if (e.Item.ItemType == ListItemType.Header)
       {
           Literal lblCat = (Literal)e.Item.FindControl("ltrlDoctors");
           lblCat.Text = "Found "+dsFindMembers.Tables[0].Rows.Count.ToString() +" doctor(s) from your search criteria. ";
       }
       if (e.Item.ItemType == ListItemType.Footer)
       {
           Label lblFoot = (Label)e.Item.FindControl("lblFooter");
           lblFoot.Text = "Changed";


       }
   }
 
Share this answer
 
Alternatively , Try

XML
<HeaderTemplate>
<asp:Label Text='<%#DataList1.Items.Count%>' runat="server" ID="lblTotal" ></asp:Label>
</headerTemplate>
 
Share this answer
 
v2
Try like this..

C#
protected void Page_Load(object sender, EventArgs e)
       {
           DataTable dt = somedataSource();
           datalist.DataSource = dt;
           datalist.DataBind();
       }

       protected void datalist_ItemDataBound(object sender, DataListItemEventArgs e)
       {
           if (e.Item.ItemType == ListItemType.Header)
           {
               Label lbl = (Label)e.Item.FindControl("lbl");
              int count = ((datalist.DataSource) as DataTable).Rows.Count;
              lbl.Text = count + " Records found";

           }

       }


aspx


ASP.NET
<asp:DataList ID="datalist" runat="server"
    onitemdatabound="datalist_ItemDataBound">
    <HeaderTemplate>
        <asp:Label ID="lbl" Text="header" runat="server"></asp:Label>
    </HeaderTemplate>
    <ItemTemplate>
        some controls
    </ItemTemplate>
</asp:DataList>
 
Share this answer
 
XML
<asp:DataList ID="datalist" runat="server"
    onitemdatabound="datalist_ItemDataBound">
    <HeaderTemplate>
        <asp:Label ID="lbl" Text="header" runat="server"></asp:Label>
    </HeaderTemplate>
    <ItemTemplate>
        some controls
    </ItemTemplate>
</asp:DataList>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900