Group Repeater for each Distinct User - c#

I have the following repeater item:
<asp:Repeater ID="RptLeaveRequests" runat="server"
onitemdatabound="RptLeaveRequests_ItemDataBound">
<ItemTemplate>
<table id="tableItem" runat="server">
<tr>
<td style="width: 100px;">
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/M/yyyy}") %>'></asp:Label>
</td>
<td style="width: 100px;">
<asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
</td>
<td style="width: 200px; font-size:10px;">
<asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
</td>
<td style="50px">
<asp:RadioButtonList ID="rbtVerified" runat="server" >
<asp:ListItem Value="1">Accept</asp:ListItem>
<asp:ListItem Value="2">Reject</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
on page load I am binding the Repeater
else if(Convert.ToInt32(Session["UserLevel"]) != 0 && Convert.ToInt32(Session["UserLevel"]) < 151)
{
RptLeaveRequests.DataSource = newLeaveLogic().GetManagerUnverifiedLeaveRequests(Convert.ToInt32(Context.User.Identity.Name));
RptLeaveRequests.DataBind();
hasRequests = true;
}
Now how can group the Repeater item for each distinct user returned by the GetManagerUnferifiedLeaveRequests which returns a DataTable in the method below:
protected void RptLeaveRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (hasRequests)
{
//grouping
}
}

I see two possible solutions:
1) Use nested repeaters:
<asp:Repeater ID="RptGroups" runat="server" onitemdatabound="RptGroups_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="RptLeaveRequests" runat="server" onitemdatabound="RptLeaveRequests_ItemDataBound">
<ItemTemplate>
...
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
2) Use single repeater for groups and render each group using for example DataList or other control for list of items.
In this case you bind list of groups to datasource here:
RptLeaveRequests.DataSource = itemsGroups;
RptLeaveRequests.DataBind();
And bind list of group items here:
protected void RptLeaveRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//bind here list of group items
}
I would prefer the second version.

Related

Highlight ListView row without re-bind or apply style to that row

Typical listview.
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" OnSelectedIndexChanging="ListView1_SelectedIndexChanging"
OnSelectedIndexChanged="ListView1_SelectedIndexChanged">
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' />
</td>
<td>
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
</td>
<td>
<asp:Button ID="cmdLstSel" runat="server" Text="View" CssClass="btn"
CommandName="Select"/>
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr style="" class="alert-info">
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' />
</td>
<td>
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
</td>
<td>
<asp:Button ID="cmdLstSel" runat="server" Text="View" CssClass="btn" OnClick="cmdLstSel_Click" />
</td>
</tr>
</SelectedItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">FirstName</th>
<th runat="server">LastName</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">View</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
So we can highlight the row clicked with:
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
}
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
GPayments.DataSource = MyRst("SELECT * FROM HotelPayments WHERE Hotel_ID = " + hID);
GPayments.DataBind();
}
Now if I leave out the re-bind, then of course the row will highlight, but of course it is the "previous" row that hight lights.
And if I could set the class of the ONE row, then I could dump the selected item template.
You can do this with GREAT ease in a GridView, and I often do this:
Dim gRow As GridViewRow = -- get any row
gRow.CssClass = " alert-info"
however, I would like to do the same with listView. When I do above for GridView, then I don't need or have to bother with a re-bind, and I don't even need a selected template either.
So left is a grid view, and we get this:
However, I want to do this with listview.
(and WITHOUT having to do a re-bind).
Any simple way to highlight a row in ListView WITHOUT a re-bind?
Ok, a bit of googling, and the solution becomes VERY easy.
The goal here is to click on a row - highlight that row.
Of course WAY too much work to have to include a selected row template.
It is a better trade off to add 3 lines of code to do this.
So, say we have this markup:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID"
OnSelectedIndexChanged="ListView1_SelectedIndexChanged" OnSelectedIndexChanging="ListView1_SelectedIndexChanging" >
<ItemTemplate>
<tr id="mytr" runat="server">
<td><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' /></td>
<td><asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' /></td>
<td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
<td>
<asp:Button ID="cmdLstSel" runat="server" Text="View" CssClass="btn"
CommandName="Select" OnClick="cmdLstSel_Click"/>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" border="0" class="table table-hover" style="">
<tr runat="server" style="">
<th runat="server">FirstName</th>
<th runat="server">LastName</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">View</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
ok, our code to fill is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadView();
}
}
void LoadView()
{
ListView1.DataSource = MyRst("SELECT TOP 12 * FROM tblHotels ORDER BY HotelName");
ListView1.DataBind();
}
Output:
Ok, so for the row click? Well, we don't have to use the selected index changed, but, for this we will (so CommandName="Select" is what triggers that index changed event).
However, I want a simple button click, and with some code (to display the details part).
So, our button click is this:
protected void cmdLstSel_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
ListViewDataItem gRow = (ListViewDataItem)btn.Parent.Parent.Parent;
int hID = (int)ListView1.DataKeys[gRow.DisplayIndex]["ID"];
GHotelOptions.DataSource = MyRst("SELECT * FROM HotelOptions WHERE Hotel_ID = " + hID);
GHotelOptions.DataBind();
GPayments.DataSource = MyRst("SELECT * FROM HotelPayments WHERE Hotel_ID = " + hID);
GPayments.DataBind();
}
Above will fill the child tables. Note the cool trick to get the row - I don't bother with the listview event model!
Now for the row highlight.
The trick is to add an "id" to the tr row like this:
<ItemTemplate>
<tr id="mytr" runat="server">
So, we can now pick up the tr element.
So on lv index changed, we do this:
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem gRow = (ListViewItem)ListView1.Items[ListView1.SelectedIndex];
if ( (ViewState["MySel"] != null) && ((int)ViewState["MySel"] != gRow.DataItemIndex) )
{
ListViewItem gLast = ListView1.Items[(int)ViewState["MySel"]];
HtmlTableRow hTRL = (HtmlTableRow)gLast.FindControl("mytr");
hTRL.Attributes.Add("class", "");
}
HtmlTableRow hTR = (HtmlTableRow)gRow.FindControl("mytr");
hTR.Attributes.Add("class", "alert-info");
ViewState["MySel"] = gRow.DataItemIndex;
}
So, I did use row state, but that lets me un-highlight + highlight, and I do NOT hve to re-bind the grid.
And I just picked a nice boot strap class - it even "reverses" the text - very nice.
So now we get this:
So this did cost about 4-5 extra lines. But, we trade that for NOT having a selected template in the lv (and that's too much to maintain both the row layout and THEN have to maintains the same for selected template. And worse, you STILL had to re-bind for the highlighted row click to show. This way, we don't.
I also used a "helper" routine to get a datatable, and that was this:
public DataTable MyRst(string strSQL)
{
DataTable rstData = new DataTable();
using (SqlCommand cmdSQL = new SqlCommand(strSQL,
new SqlConnection(Properties.Settings.Default.TEST3)))
{
cmdSQL.Connection.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
return rstData;
}
At the end of the day, we can now highlight the row, do so with a click, and we could even move the selected index changed code to our button code and not even use the lv index and built in events.

Change repeater border colour through code

I have a repeater which is visible only by managers where they can accept/reject leave requests made by the users.
The following code while binding my repeater, retrieves if there are any employees on leave working in the same department and on the same date as the one requested by the user.
protected void RptLeaveRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
otherEmpsLeave = new LeaveLogic().CheckEmployeeLeaveOnDateInSameDep(date, currEmp);
if (otherEmpsLeave != "")
{
//change border of repeater row to another colour and display otherEmpsLeave when manager hovers on repeater row
}
}
How can I change repeater row border colour (ex add a red border to make it stand out) and display otherEmpsLeave on hover?
ASP Repeater Code:
<h3>LEAVE REQUESTS</h3>
<asp:Label ID="LblNoRequests" Visible="false" Font-Size="14px" runat="server"></asp:Label>
<asp:HiddenField ID="dataGroups" runat="server" />
<asp:Repeater ID="RptLeaveRequests" runat="server"
onitemdatabound="RptLeaveRequests_ItemDataBound">
<ItemTemplate>
<table id="tableItem" runat="server">
<tr>
<td style="width:400px;">
<asp:Label ID="lblEmployeeId" runat="server" Text='<%#Eval("EmployeeId") %>' Visible="false" />
<asp:HiddenField ID="HdnEmployeeId" runat="server" Value='<%#Eval("EmployeeId") %>' />
<asp:Literal Text="" runat="server" ID="LiteralUser" ></asp:Literal>
</td>
</tr>
<tr>
<td style="width: 100px;">
<asp:HiddenField ID="HdnRequestId" runat="server" Value='<%#Eval("id") %>' />
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/MM/yyyy}") %>'></asp:Label>
</td>
<td style="width: 80px;">
<asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
</td>
<td style="width: 50px; font-size:10px;">
<asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
</td>
<td style="width: 850px; font-size:10px;">
<asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
</td>
<td style="width: 50px;">
<asp:RadioButtonList ID="rbtVerified" runat="server" Visible='<%#!(Boolean)Eval("ReadOnly") %>' >
<asp:ListItem Value="1">Accept</asp:ListItem>
<asp:ListItem Value="2">Reject</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
<div style="border-style: dotted; border-color:Black; width:100%; border-width:1px; border-color:#999999"></div>
</ItemTemplate>
</asp:Repeater>
From the MSDN example, something like
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
// This event is raised for the header, the footer, separators, and items.
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
if (((Evaluation)e.Item.DataItem).Rating == "Good") {
((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
}
}
}
instead of setting .Text set whatever property(s) you need, such as Background or Tooltip

How to databind to a Gridview where column values should appear in sub rows within a main row?

Please can anyone suggest a way to databind a database table to a gridview but in a specific layout where column values should appear in sub rows within a main row, like wise all rows from the database table should list down inside that Gridview.I know the normal way of databinding to a gridview Where it display row data(From Database Table) as columns, but what I want is like below
Gridview is not suitable for this type of format.
If you are okay with repeater, you can do something like this:
And here's a link to MSDN: Repeater Class.
UPDATE: If you want to post each answer selection, you can use option button and group them. You can use questionid as a part of group name and in the code get the question id. Your markup may look like below:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th colspan="2"></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "QuestionId") %> </td>
<td>
<table>
<tr>
<td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Question") %> </td>
</tr>
<tr>
<td>
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer1") %>'
OnCheckedChanged="RadioButton1_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer2") %>'
OnCheckedChanged="RadioButton2_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer3") %>'
OnCheckedChanged="RadioButton3_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
<td><asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer4") %>'
OnCheckedChanged="RadioButton4_CheckedChanged" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' TextAlign="Left" />
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And in the code:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
string grpId = ((RadioButton)sender).GroupName;
int questionId = 0;
int.TryParse(grpId.Split('_')[1].ToString(), out questionId);
//Use questionId
}

Retrieve Data from Repeater item

I have the following Repeater:
<asp:Repeater ID="RptLeaveRequests" runat="server"
onitemdatabound="RptLeaveRequests_ItemDataBound">
<ItemTemplate>
<table id="tableItem" runat="server">
<tr>
<td style="width: 100px;">
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/M/yyyy}") %>'></asp:Label>
</td>
<td style="width: 100px;">
<asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
</td>
<td style="width: 200px; font-size:10px;">
<asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
</td>
<td style="50px">
<asp:RadioButtonList ID="rbtVerified" runat="server" >
<asp:ListItem Value="1">Accept</asp:ListItem>
<asp:ListItem Value="2">Reject</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Am I trying to loop through each item, find one where the radio button is checked. If a radio button is checked i want to then check its value (Accept or Reject) and retrieve the data (Eval Date,Hours etc) and send it to another method to add to the database.
Can you please help me out, code so far:
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in RptLeaveRequests.Items)
{
}
}
You use <asp:RadioButtonList> , but in your code behind , you cast as RadioButton .
Try likes this ,
foreach (RepeaterItem item in RptLeaveRequests.Items)
{
var rdbList = item.FindControl("rbtVerified") as RadioButtonList;
switch(rdbList.SelectedValue)
{
case "1":
//Accept
break;
case "2":
//Reject
break;
}
}

GridView: Hide Sorting Arrows on Header

I am having difficulties hiding the sorting arrows on a column header that is not sortable.
I clearly define my sortable columns in the HeaderTemplate sections, and non sortable columns in the ItemTemplate, yet the sorting arrows still appears for the second TemplateField defined below. What am i Missing?
<asp:GridView ID="gvBeneficiary" runat="server" Width="100%" AllowPaging="True" CssClass="gridheader"
EmptyDataText=""
AutoGenerateColumns="False" PageSize="10"
OnPageIndexChanging="gvBeneficiary_PageIndexChanging" OnRowCommand="gvBeneficiary_RowCommand"
OnRowDataBound="gvBeneficiary_RowDataBound" HeaderStyle-CssClass="lhs">
<Columns>
<asp:TemplateField HeaderStyle-CssClass="lhs">
<HeaderTemplate>
<asp:LinkButton ID="lnkFullName" Text="Full Name"
CommandName="Sort" CommandArgument="FullName" runat="server"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<%#Eval("FullName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="EditClick" CommandArgument='<%#Eval("RecipientID") %>'
Text="Edit">
</asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="DeleteClick" CommandArgument='<%#Eval("RecipientID") %>'
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
What do u mean by defining non sortable columns in the ItemTemplate ?
Specify Allowsorting =true for the grid asd sort expression for respective columns which u want sorting to be enabled.
<asp:GridView AllowSorting="true"......../> and
<asp:TemplateField.............. SortExpression ="Full Name">
And don't specify the sort expression for columns u don't need sorting.
I think this is enough for sorting specified columns.
Is there a reason you can't use the default sorting ability in the gridview? If you can, apply the "arrows" CSS to all hyperlinks in a table header cell
.gvclass th a {background-image...}
That way all headers with a SortExpression will have those CSS properties and all non-sortable headers will not (as no hyperlink will be generated in those cells).
If you just want to disable sorting for a specific column in an event, normally it should work to set the GridViewColumn's SortExpression to null:
gvBeneficiary.Columns[0].SortExpression = null;
I have done this using listview.Please try it with Gridview
my html code is
<asp:ListView ID="lst_Area" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_Area_ItemDataBound">
<LayoutTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td width="35px" align="left">
S.No
</td>
<td align="left" width="400px">
<asp:LinkButton ID="lnk_Name" runat="server" CommandArgument="tblAreaNew.AreaName"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Name</asp:LinkButton>
<asp:Image ID="img_lnk_Name" Visible="false" runat="server" />
</td>
<td align="left" width="250px">
<asp:LinkButton ID="lnk_Location" runat="server" CommandArgument="tblAreaNew.Locationid"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Location</asp:LinkButton>
<asp:Image ID="img_lnk_Location" Visible="false" runat="server" />
</td>
<td align="left" width="175px">
<asp:LinkButton ID="lnk_CreatedBy" runat="server" CommandArgument="v.FirstName" ValidationGroup="vgSearch"
OnClick="lnk_Sort">Created By</asp:LinkButton>
<asp:Image ID="img_lnk_CreatedBy" Visible="false" runat="server" />
</td>
<td align="left" width="120px">
<asp:LinkButton ID="lnk_CreatedOn" runat="server" CommandArgument="tblAreaNew.createddate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Created On</asp:LinkButton>
<asp:Image ID="img_lnk_CreatedOn" Visible="false" runat="server" />
</td>
<td align="left" width="175px">
<%--<asp:LinkButton ID="lnkCreatedDate" runat="server" CommandArgument="tblUserActivities.CreatedDate"
OnClick="lnk_Sort">Created Date</asp:LinkButton>--%>
<asp:LinkButton ID="lnk_LastModfiedBy" runat="server" CommandArgument="v.FirstName"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified By</asp:LinkButton>
<asp:Image ID="img_lnk_LastModfiedBy" Visible="false" runat="server" />
</td>
<td align="left" width="120px">
<asp:LinkButton ID="lnk_LastModfiedOn" runat="server" CommandArgument="tblAreaNew.ModifiedDate"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified On</asp:LinkButton>
<asp:Image ID="img_lnk_LastModfiedOn" Visible="false" runat="server" />
</td>
<td align="center" width="50px">
<asp:LinkButton ID="lnk_Status" runat="server" CommandArgument="tblAreaNew.isactive"
ValidationGroup="vgSearch" OnClick="lnk_Sort">Status</asp:LinkButton>
<asp:Image ID="img_lnk_Status" Visible="false" runat="server" />
</td>
<td align="center" width="50px" style="border-right: 1px solid #6398cc;">
Activity
<%-- <div style="width: 50px; float: right;">
</div>--%>
</td>
</tr>
<tr id="tr" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="left" valign="middle">
<%# Container.DataItemIndex+1 %>.
</td>
<td align="left">
<asp:Label ID="lblAreaID" runat="server" Text='<%# Eval("Areaid")%>' Visible="false"></asp:Label>
<%# Eval("AreaName")%>
</td>
<td align="left">
<%# Eval("Location")%>
</td>
<td align="left">
<%# Eval("CreatedBy")%>
</td>
<td align="left">
<%# Convert.ToDateTime(Eval("CreatedDate")).ToString("MMM, dd yyyy")%>
</td>
<td align="left">
<%# Eval("ModifiedBy")%>
</td>
<td align="left">
<%# Convert.ToDateTime(Eval("ModifiedDate")).ToString("MMM, dd yyyy")%>
</td>
<td align="center">
<asp:Label ID="lblLocationIsActive" runat="server" Style="display: none;" Text='<%# Eval("LocationIsActive")%>'></asp:Label>
<asp:Label ID="lbl_Status" runat="server" Style="display: none;" Text='<%# Eval("IsActive")%>'></asp:Label>
<asp:ImageButton ID="imgbtnStatus" runat="server" CommandArgument='<%# Eval("Areaid") %>'
OnClick="imgbtnStatus_Onclick" />
</td>
<td class="last" align="center">
<asp:Label ID="lblAreaName" runat="server" Style="display: none;" Text='<%# Eval("AreaName")%>'></asp:Label>
<asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/edit.png"
ToolTip="Edit Details" CommandArgument='<%# Eval("AreaId") %>' OnClick="imgbtnEdit_OnClick" />
<asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/delete.png"
ToolTip="Delete" CommandArgument='<%# Eval("AreaId") %>' Visible="false" OnClientClick="return confirm('Are you sure you want to delete the location?');"
OnClick="imgbtnDelete_OnClick" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
And my code behind code is
protected void lnk_Sort(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
string arg = lnk.CommandArgument.ToString();
ViewState["sortCol"] = arg;
GetSortDirection();
BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), Pager.PageSize);
string name = lnk.ID;
Image img = (Image)(lst_Area.FindControl("img_" + name));
if (img != null)
{
SetSortOrderImage(img, ViewState["sortDir"].ToString());
}
}
private void SetSortOrderImage(Image image, String sortorder)
{
if (sortorder == "asc")
{
image.Visible = true;
image.ImageUrl = "../App_Themes/ThemeNew2/images/up.png";
}
else if (sortorder == "Desc")
{
image.Visible = true;
image.ImageUrl = "../App_Themes/ThemeNew2/images/down.png";
}
}
/// <summary>
/// this method get the sort direction
/// </summary>
private void GetSortDirection()
{
if (Convert.ToString(ViewState["sortDir"]) == "Desc")
{
ViewState["sortDir"] = "asc";
}
else
{
ViewState["sortDir"] = "Desc";
}
}
Hope this will work for you.one this more i would like to let you know the inbuilt sorting and paging are slower for gridview as compare to custom paging and sorting.

Categories