private string GetSortQuery(ASPRazorWebGridColumn item)
        {
            var gridParams         = GridParameters.GetGridParameters(GridID);
            var querySource        = UsePostBack ? HttpContext.Current.Request.Form : HttpContext.Current.Request.QueryString;
            var applySortDirection = SortDirection.Ascending;

            var currentSort = gridParams.Sort;

            if (!string.IsNullOrEmpty(currentSort) && (currentSort == item.DataField || currentSort == item.SortExpression))
            {
                var currentSortDirection = gridParams.SortDirection;
                applySortDirection = currentSortDirection == "DESC" ? SortDirection.Ascending : SortDirection.Descending;
            }

            var sortQuery = "";

            if (!UsePostBack)
            {
                sortQuery += HttpContext.Current.Request.Path + string.Format("?{0}={1}&{2}={3}", UrlEncode(_sortFieldName)
                                                                              , UrlEncode(item.SortExpression ?? item.DataField)
                                                                              , UrlEncode(_sortDirectionFieldName)
                                                                              , UrlEncode(applySortDirection == SortDirection.Ascending ? "ASC" : "DESC"));
            }
            else
            {
                sortQuery = string.Format("{0}={1}&{2}={3}", _sortFieldName
                                          , item.SortExpression ?? item.DataField
                                          , _sortDirectionFieldName
                                          , applySortDirection == SortDirection.Ascending ? "ASC" : "DESC");
            }

            return(sortQuery);
        }
        public static GridParameters GetGridParameters(string gridID, List <string> validSortColumns = null)
        {
            var paramSource = HttpContext.Current.Request;
            var queryKeys   = HttpContext.Current.Request.QueryString.AllKeys;
            var formKeys    = HttpContext.Current.Request.Form.AllKeys;
            var pageKey     = string.Join("_", gridID, "page");
            var sortKey     = string.Join("_", gridID, "sort");
            var sortDirKey  = string.Join("_", gridID, "sortdir");

            GridParameters parameters = (GridParameters)HttpContext.Current.Session[string.Join("_", gridID, "gridparameters")];

            if (parameters == null)
            {
                parameters = new GridParameters();
            }

            if (queryKeys.Contains(pageKey) || formKeys.Contains(pageKey))
            {
                try
                {
                    parameters.Page = Int32.Parse(paramSource[pageKey]);
                }
                catch { }
            }

            if (queryKeys.Contains(sortKey) || formKeys.Contains(sortKey))
            {
                parameters.Sort = paramSource[sortKey];
            }

            if (queryKeys.Contains(sortDirKey) || formKeys.Contains(sortDirKey))
            {
                parameters.SortDirection = paramSource[sortDirKey];
            }

            if (validSortColumns != null &&
                validSortColumns.Count > 0 &&
                !validSortColumns.Contains(paramSource[sortKey]))
            {
                parameters.Sort =
                    parameters.SortDirection = null;
            }

            parameters.Sort          = parameters.Sort == "" ? null : parameters.Sort;
            parameters.SortDirection = parameters.SortDirection == "" ? null : parameters.SortDirection;

            return(parameters);
        }
        private string AddGridParametersPostbackHtml(string resultHtml)
        {
            var form = new TagBuilder("form");

            form.MergeAttribute("method", "post");
            form.MergeAttribute("action", HttpContext.Current.Request.RawUrl);

            var gridParams = GridParameters.GetGridParameters();

            form.InnerHtml += CreateHiddenInput(_pageFieldName, Convert.ToString(gridParams.Page)).ToString();
            form.InnerHtml += CreateHiddenInput(_sortFieldName, gridParams.Sort).ToString();
            form.InnerHtml += CreateHiddenInput(_sortDirectionFieldName, gridParams.SortDirection).ToString();
            form.InnerHtml += resultHtml;

            return(form.ToString());
        }
        public IHtmlString GetHtml(ASPRazorWebGridColumn[] columns = null, string tableStyle = null, string headerStyle = null, string alternatingRowStyle = null, Pager pager = null)
        {
            //Pager = pager;
            //Pager.Container = this;
            var table = new TagBuilder("table");

            if (!string.IsNullOrEmpty(Width))
            {
                table.MergeAttribute("style", string.Format("width: {0}", Width));
            }

            if (!string.IsNullOrEmpty(tableStyle))
            {
                table.Attributes["class"] = tableStyle;
            }

            if (!string.IsNullOrEmpty(tableStyle))
            {
                table.Attributes["id"] = "example1";
            }

            if (columns.Count() > 0)
            {
                //create top pager
                //if (Pager != null && (Pager.Position == PagerPosition.Top || Pager.Position == PagerPosition.TopAndBottom))
                //    Pager.AddPager(table, columns.Count());

                //create header
                var headerRow = new TagBuilder("tr");
                if (!string.IsNullOrEmpty(headerStyle))
                {
                    headerRow.Attributes["class"] = headerStyle;
                }

                foreach (var item in columns)
                {
                    var cell = new TagBuilder("th");

                    var style = "";
                    style += !string.IsNullOrEmpty(item.Width) ? string.Format("width: {0};", item.Width) : "";
                    style += !string.IsNullOrEmpty(item.HeaderHorizontalAlignment) ? string.Format("text-align: {0};", item.HeaderHorizontalAlignment) : "";
                    style += !string.IsNullOrEmpty(item.HeaderVerticalAlignment) ? string.Format("vertical-align: {0};", item.HeaderVerticalAlignment) : "";

                    if (!string.IsNullOrEmpty(style))
                    {
                        cell.MergeAttribute("style", style);
                    }

                    if (!string.IsNullOrEmpty(item.DataField))
                    {
                        cell.SetInnerText(item.HeaderText ?? item.DataField);
                    }
                    else if (!string.IsNullOrEmpty(item.HeaderText))
                    {
                        cell.SetInnerText(item.HeaderText);
                    }

                    if (_source.Count() > 0 && item.CanSort && (!string.IsNullOrEmpty(item.DataField) || !string.IsNullOrEmpty(item.HeaderText)))
                    {
                        cell.SetInnerText("");
                        var sortTag = new TagBuilder("a");

                        sortTag.SetInnerText(item.HeaderText ?? item.DataField);

                        if (!UsePostBack)
                        {
                            sortTag.MergeAttribute("href", GetSortQuery(item));
                        }
                        else
                        {
                            var uri = new Uri("http://www.dummyuri.org/?" + GetSortQuery(item));
                            var nvc = HttpUtility.ParseQueryString(uri.Query);
                            sortTag.MergeAttribute("href", "javascript:;");
                            sortTag.MergeAttribute("onclick", "$(\"#" + _sortFieldName + "\").val(\"" + nvc[_sortFieldName] + "\");$(\"#" + _sortDirectionFieldName + "\").val(\"" + nvc[_sortDirectionFieldName] + "\");$(\"#" + _pageFieldName + "\").val(\"\");$(this).parents(\"form:first\").submit();");
                        }

                        cell.InnerHtml = sortTag.ToString();

                        //add sort direction glyph
                        var gridParameters = GridParameters.GetGridParameters(GridID);
                        if (gridParameters.Sort != null &&
                            (gridParameters.Sort == item.DataField || gridParameters.Sort == item.SortExpression))
                        {
                            var img     = new TagBuilder("img");
                            var imgfile = gridParameters.SortDirection == "ASC" ? "asc.gif" : "desc.gif";
                            img.MergeAttribute("src", "/areas/admin/content/images/" + imgfile);
                            img.MergeAttribute("style", "vertical-align: bottom; margin-left: 2px;");

                            cell.InnerHtml += img.ToString();
                        }
                    }

                    headerRow.InnerHtml += cell;
                }

                table.InnerHtml += "<thead>" + headerRow.ToString() + "</thead>";

                //create rows
                bool rowNormal = true;
                table.InnerHtml += " <tbody>";
                foreach (var item in _source)
                {
                    var row = new TagBuilder("tr");

                    if (!rowNormal && !string.IsNullOrEmpty(alternatingRowStyle))
                    {
                        row.Attributes["class"] = alternatingRowStyle;
                    }

                    rowNormal = !rowNormal;

                    foreach (var col in columns)
                    {
                        var cell = new TagBuilder("td");

                        var style = "";
                        style += !string.IsNullOrEmpty(col.Width) ? string.Format("width: {0};", col.Width) : "";
                        style += !string.IsNullOrEmpty(col.ItemHorizontalAlignment) ? string.Format("text-align: {0};", col.ItemHorizontalAlignment) : "";
                        style += !string.IsNullOrEmpty(col.ItemVerticalAlignment) ? string.Format("vertical-align: {0};", col.ItemVerticalAlignment) : "";

                        if (!string.IsNullOrEmpty(style))
                        {
                            cell.MergeAttribute("style", style);
                        }

                        if (!string.IsNullOrEmpty(col.DataField))
                        {
                            cell.SetInnerText(GetDynamicMemberValue(item, col.DataField).ToString());
                        }

                        if (col.DataFieldFormat != null)
                        {
                            cell.InnerHtml += col.DataFieldFormat.Invoke(item);
                        }

                        row.InnerHtml += cell;
                    }

                    table.InnerHtml += row.ToString();
                }
                table.InnerHtml += " </tbody>";

                //create bottom pager
                //if (Pager != null && (Pager.Position == PagerPosition.Bottom || Pager.Position == PagerPosition.TopAndBottom))
                //    Pager.AddPager(table, columns.Count());
            }

            if (_maintainState)
            {
                SaveGridParameters();
            }

            var resultHtml = table.ToString();

            //create hidden field for grid parameters
            if (UsePostBack)
            {
                resultHtml = AddGridParametersPostbackHtml(resultHtml);
            }

            return(new HtmlString(resultHtml));
        }
        public void SaveGridParameters()
        {
            var gridParams = GridParameters.GetGridParameters(GridID);

            HttpContext.Current.Session[string.Join("_", GridID, "gridparameters")] = gridParams;
        }