Sunday, December 22, 2013

RadGrid with List

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadGrids1.aspx.cs" Inherits="RadGrids1" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>


<!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>
<telerik:RadCodeBlock ID="RadCodeBlock2" runat="server">
<style type="text/css">
.orderText
{
font: normal 12px Arial,Verdana;
margin-top: 6px;
}
</style>
</telerik:RadCodeBlock>

<script type="text/javascript">

function ShowInsertForm() {
window.radopen("EditUserDetail.aspx", "UserListDialog");
return false;
}

function ShowEditForm(id, rowIndex) {
window.radopen("EditUserDetail.aspx?UserID=" + id, "UserListDialog");
return false;
}


function RowDblClick(sender, eventArgs) {
window.radopen("EditUserDetail.aspx?UserID=" + eventArgs.getDataKeyValue("EmployeeID"), "UserListDialog");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<telerik:RadSkinManager ID="QsfSkinManager" runat="server" ShowChooser="true" />
<telerik:RadFormDecorator ID="QsfFromDecorator" runat="server" DecoratedControls="All" EnableRoundedCorners="false" />


<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>



<telerik:RadAjaxLoadingPanel runat="server" ID="gridLoadingPanel"></telerik:RadAjaxLoadingPanel>
<telerik:RadGrid OnItemCreated="RadGrid1_ItemCreated" ID="RadGrid1" runat="server" AllowPaging="false" Width="97%">

<PagerStyle Mode="NumericPages"></PagerStyle>
<MasterTableView AutoGenerateColumns="False" DataKeyNames="Id" ClientDataKeyNames="Id"
Width="100%" CommandItemDisplay="Top" PageSize="5">
<Columns>

<telerik:GridTemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server"></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="FName">
<ItemTemplate>
<asp:Label ID="lblFName" runat="server"></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="DOB">
<ItemTemplate>
<asp:Label ID="lblDOB" runat="server"></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>

<telerik:GridTemplateColumn UniqueName="TemplateEditColumn">
<ItemTemplate>
<asp:HyperLink ID="EditLink" runat="server" Text="Add"></asp:HyperLink>
<asp:HyperLink ID="EditLink2" runat="server" Text="Edit"></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<CommandItemTemplate>
<a href="#" onclick="return ShowInsertForm();">Add New Record</a>

</CommandItemTemplate>
</MasterTableView>

</telerik:RadGrid>

<asp:DataList CellPadding="5" RepeatDirection="Horizontal" runat="server" ID="dlPager"
OnItemCommand="dlPager_ItemCommand" CssClass="grid_paging">
<ItemTemplate>
<asp:LinkButton Enabled='<%#Eval("Enabled") %>' runat="server" ID="lnkPageNo" Text='<%#Eval("Text") %>'
CommandArgument='<%#Eval("Value") %>' CommandName="PageNo"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>


<telerik:RadWindowManager ID="RadWindowManager1" runat="server" EnableShadow="true">
<Windows>
<telerik:RadWindow ID="UserListDialog" runat="server" Title="Editing record" Height="320px"
Width="310px" Left="150px" ReloadOnShow="true" ShowContentDuringLoad="false"
Modal="true">
</telerik:RadWindow>
</Windows>
</telerik:RadWindowManager>


</ContentTemplate>
</asp:UpdatePanel>

<div>

</div>

</form>
</body>
</html>




RadGrid1.aspx.cs


--------------------




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class RadGrids1 : System.Web.UI.Page
{

#region PAGE_LOAD EVENTS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid(1);
}

}
#endregion

#region PRIVATE METHOD
private void BindGrid(int currentPage)
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() { ID = 1, FName = "John", DOB = DateTime.Parse("12/11/1971") });
empList.Add(new Employee() { ID = 2, FName = "Mary", DOB = DateTime.Parse("01/17/1961") });
empList.Add(new Employee() { ID = 3, FName = "Amber", DOB = DateTime.Parse("12/23/1971") });
empList.Add(new Employee() { ID = 4, FName = "Kathy", DOB = DateTime.Parse("11/15/1976") });
empList.Add(new Employee() { ID = 5, FName = "Lena", DOB = DateTime.Parse("05/11/1978") });
empList.Add(new Employee() { ID = 6, FName = "John1", DOB = DateTime.Parse("12/11/1971") });
empList.Add(new Employee() { ID = 7, FName = "Mary1", DOB = DateTime.Parse("01/17/1961") });
empList.Add(new Employee() { ID = 8, FName = "Amber1", DOB = DateTime.Parse("12/23/1971") });
empList.Add(new Employee() { ID = 9, FName = "Kathy1", DOB = DateTime.Parse("11/15/1976") });
empList.Add(new Employee() { ID = 10, FName = "Lena1", DOB = DateTime.Parse("05/11/1978") });
empList.Add(new Employee() { ID = 11, FName = "John2", DOB = DateTime.Parse("12/11/1971") });
empList.Add(new Employee() { ID = 12, FName = "Mary2", DOB = DateTime.Parse("01/17/1961") });
empList.Add(new Employee() { ID = 13, FName = "Amber2", DOB = DateTime.Parse("12/23/1971") });
empList.Add(new Employee() { ID = 14, FName = "Kathy2", DOB = DateTime.Parse("11/15/1976") });
empList.Add(new Employee() { ID = 15, FName = "Lena2", DOB = DateTime.Parse("05/11/1978") });
empList.Add(new Employee() { ID = 16, FName = "John2", DOB = DateTime.Parse("12/11/1971") });
empList.Add(new Employee() { ID = 17, FName = "Mary2", DOB = DateTime.Parse("01/17/1961") });
empList.Add(new Employee() { ID = 18, FName = "Amber2", DOB = DateTime.Parse("12/23/1971") });
empList.Add(new Employee() { ID = 19, FName = "Kathy2", DOB = DateTime.Parse("11/15/1976") });
empList.Add(new Employee() { ID = 20, FName = "Lena2", DOB = DateTime.Parse("05/11/1978") });
empList.Add(new Employee() { ID = 21, FName = "John3", DOB = DateTime.Parse("12/11/1971") });
empList.Add(new Employee() { ID = 22, FName = "Mary3", DOB = DateTime.Parse("01/17/1961") });
empList.Add(new Employee() { ID = 23, FName = "Amber3", DOB = DateTime.Parse("12/23/1971") });
empList.Add(new Employee() { ID = 24, FName = "Kathy3", DOB = DateTime.Parse("11/15/1976") });
empList.Add(new Employee() { ID = 25, FName = "Lena3", DOB = DateTime.Parse("05/11/1978") });
empList.Add(new Employee() { ID = 26, FName = "John3", DOB = DateTime.Parse("12/11/1971") });
empList.Add(new Employee() { ID = 27, FName = "Mary3", DOB = DateTime.Parse("01/17/1961") });
empList.Add(new Employee() { ID = 28, FName = "Amber3", DOB = DateTime.Parse("12/23/1971") });
empList.Add(new Employee() { ID = 29, FName = "Kathy3", DOB = DateTime.Parse("11/15/1976") });
empList.Add(new Employee() { ID = 30, FName = "Lena3", DOB = DateTime.Parse("05/11/1978") });
empList.Add(new Employee() { ID = 31, FName = "John4", DOB = DateTime.Parse("12/11/1971") });
empList.Add(new Employee() { ID = 32, FName = "Mary4", DOB = DateTime.Parse("01/17/1961") });
empList.Add(new Employee() { ID = 33, FName = "Amber4", DOB = DateTime.Parse("12/23/1971") });
empList.Add(new Employee() { ID = 34, FName = "Kathy4", DOB = DateTime.Parse("11/15/1976") });
empList.Add(new Employee() { ID = 35, FName = "Lena4", DOB = DateTime.Parse("05/11/1978") });
empList.Add(new Employee() { ID = 36, FName = "John4", DOB = DateTime.Parse("12/11/1971") });
empList.Add(new Employee() { ID = 37, FName = "Mary4", DOB = DateTime.Parse("01/17/1961") });
empList.Add(new Employee() { ID = 38, FName = "Amber4", DOB = DateTime.Parse("12/23/1971") });
empList.Add(new Employee() { ID = 39, FName = "Kathy4", DOB = DateTime.Parse("11/15/1976") });
empList.Add(new Employee() { ID = 40, FName = "Lena4", DOB = DateTime.Parse("05/11/1978") });
int TotalCount = empList.Count();
//var records = from emp in empList
// select emp;
var pgNo = currentPage;
var pgRec = 10;
empList = empList.Skip((pgNo - 1) * pgRec).Take(pgRec).ToList();
RadGrid1.DataSource = empList;
RadGrid1.DataBind();


generatePager(TotalCount, pgRec, pgNo);
}
#endregion

#region RADGRID EVENTS
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
Employee row = (Employee)e.Item.DataItem;
if (row != null)
{
Label lblID = (Label)e.Item.FindControl("lblID");
Label lblFName = (Label)e.Item.FindControl("lblFName");
Label lblDOB = (Label)e.Item.FindControl("lblDOB");

lblID.Text = row.ID.ToString();
lblFName.Text = row.FName;
lblDOB.Text = row.DOB.ToString();

HyperLink editLink = (HyperLink)e.Item.FindControl("EditLink");
editLink.Attributes["href"] = "javascript:void(0);";
editLink.Attributes["onclick"] = String.Format("return ShowInsertForm();");
//editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"], e.Item.ItemIndex);


HyperLink editLink2 = (HyperLink)e.Item.FindControl("EditLink2");
editLink2.Attributes["href"] = "javascript:void(0);";
editLink2.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", lblID.Text, e.Item.ItemIndex);
//editLink2.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"], e.Item.ItemIndex);

}
}
}

protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
if (e.Argument == "Rebind")
{
//RadGrid1.MasterTableView.SortExpressions.Clear();
//RadGrid1.MasterTableView.GroupByExpressions.Clear();
//RadGrid1.Rebind();
BindGrid(1);
}
else if (e.Argument == "RebindAndNavigate")
{
//RadGrid1.MasterTableView.SortExpressions.Clear();
//RadGrid1.MasterTableView.GroupByExpressions.Clear();
//RadGrid1.MasterTableView.CurrentPageIndex = RadGrid1.MasterTableView.PageCount - 1;
//RadGrid1.Rebind();
BindGrid(1);
}
}
#endregion

#region PAGER
public void generatePager(int totalRowCount, int pageSize, int currentPage)
{
int totalLinkInPage = 3;
int totalPageCount = (int)Math.Ceiling((decimal)totalRowCount / pageSize);
int startPageLink = Math.Max(currentPage - (int)Math.Floor((decimal)totalLinkInPage / 2), 1);
int lastPageLink = Math.Min(startPageLink + totalLinkInPage - 1, totalPageCount);
if ((startPageLink + totalLinkInPage - 1) > totalPageCount)
{
lastPageLink = Math.Min(currentPage + (int)Math.Floor((decimal)totalLinkInPage / 2), totalPageCount);
startPageLink = Math.Max(lastPageLink - totalLinkInPage + 1, 1);
}
List<ListItem> pageLinkContainer = new List<ListItem>();

if (startPageLink != 1)
{
int prevcounts = currentPage - 1;
pageLinkContainer.Add(new ListItem("First", prevcounts.ToString(), currentPage != 1));
}
for (int i = startPageLink; i <= lastPageLink; i++)
{
pageLinkContainer.Add(new ListItem(i.ToString(), i.ToString(), currentPage != i));
}
if (lastPageLink != totalPageCount)
{
int Nextcounts = currentPage + 1;
pageLinkContainer.Add(new ListItem("Last", Nextcounts.ToString(), currentPage != totalPageCount));
}

dlPager.DataSource = pageLinkContainer;
dlPager.DataBind();
}


protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "PageNo")
{
BindGrid(Convert.ToInt32(e.CommandArgument));
}
}
#endregion

#region CLASS EMPLOYEE
class Employee
{
public int ID { get; set; }
public string FName { get; set; }
public DateTime DOB { get; set; }
}
#endregion

}

0 comments:

Post a Comment