<asp:ImageButton ID="lnkEmpIdUp" runat="server" ImageUrl="~/Images/upArrow.png" CommandName="EMP_ID_NO" OnClick="lnkSorting_Click" />
<asp:ImageButton ID="lnkEmpIdDown" runat="server" ImageUrl="~/Images/downArrow.png" CommandName="EMP_ID_NO" OnClick="lnkSorting_Click" />
As the above code says, there are 2 images button (Kept in Gridview header template) which clicked, would perform sorting. Close look would result that both the controls have same command name and same onClick Event.
The OnClick Event handles the column to be sorted through command name and the sorting direction is handled through hidden field. See below code
protected void lnkSorting_Click(object sender, EventArgs e)
{
// Initialize variables
//Get Dataset values here for the grid.
var imgSort = sender as ImageButton;
string colName = imgSort.CommandName;
if (imgSort.ImageUrl.Trim().ToUpper().Contains(("up").ToUpper())) // If Up(Ascending)arrow is clicked.
{
if (hdnSortDir.Value.Equals(string.Empty) || hdnSortDir.Value.ToString().Equals("desc"))
{
hdnSortDir.Value = "asc";
//imgSort.ImageUrl = "~/Images/ascending.gif";
}
}
else if (imgSort.ImageUrl.Trim().ToUpper().Contains(("down").ToUpper()))
{
hdnSortDir.Value = "desc";
}
.....Sorting Logic...
}
My Doubt: How can i change the image at run time when the sorting is performed in gridview? Say after sorting the column "Employee Name" ascending, the ascending image for that column should change to some other image so that user could identify as to which column is sort and in which direction.
Please help!!!
Thanks!