Often I’ve had to build a page that didn’t allow the datagrid or listview built in sort options so I came up with this function so that you can keep filters and change the sorting and direction of the page.
public string sPageURLwVariables(string sArrow, string sDir)
{
string sOriginalURL = HttpContext.Current.Request.Url.PathAndQuery;
string sCleanURL = "";
string[] sURLVariables = sOriginalURL.Split(new char[] { '?', '&' });
if (sURLVariables.GetLength(0) > 0)
{
foreach (string sURLVariable in sURLVariables)
{
if (sURLVariable != HttpContext.Current.Request.Url.AbsolutePath)
{
if (sURLVariable.IndexOf(sArrow, 0) < 1)
{
if (sCleanURL != "")
{
sCleanURL += "&";
}
sCleanURL += sURLVariable;
Response.Write(sURLVariable + "<br>");
}
}
}
if (sCleanURL != "")
{
sCleanURL = HttpContext.Current.Request.Url.AbsolutePath + "?" + sCleanURL;
}
else
{
sCleanURL = HttpContext.Current.Request.Url.AbsolutePath;
}
return sCleanURL;
}
else
{
return HttpContext.Current.Request.Url.AbsolutePath;
}
}
Originally Posted on June 28, 2013
Last Updated on October 26, 2015
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.