private void SetSortHeaderAttributes(ASP.GridViewRowEventArgs e) { bool images = (this.AscImage != String.Empty && this.DescImage != String.Empty); for (int i = 0; i < e.Row.Cells.Count; i++) { ASP.TableCell td = e.Row.Cells[i]; if (td.HasControls()) { ASP.LinkButton button = td.Controls[0] as ASP.LinkButton; if (button != null) { if (this.SortExpression == button.CommandArgument) { td.ApplyStyle(this.SortedColumnHeaderRowStyle); this.sortColumnIndex = i; if (images) { ImageButton btn = new ImageButton(); btn.CommandName = button.CommandName; btn.CommandArgument = button.CommandArgument; btn.ImageUrl = (this.SortDirection == ASP.SortDirection.Ascending ? this.AscImage : this.DescImage); td.Controls.Add(new LiteralControl(" ")); td.Controls.Add(btn); td.Controls.Add(new LiteralControl(" ")); } } } } } }
private static void RenderInnerControlContent(TableCell tableCell, ExportCell cell) { if (!tableCell.HasControls()) { return; } using (var writer = new StringWriter()) using (var html = new HtmlTextWriter(writer)) { tableCell.Controls.OfType<Control>() .Where(c => c.Visible) .Where(c => !(c is HiddenField)) .ForEach(c => SetContent(c, cell, html)); cell.Markup += writer.ToString(); } cell.Text += HtmlTagRegex.Replace(cell.Markup, String.Empty); }
public static void SetGridViewSortImages(object sender, GridViewRowEventArgs e, string sortExpression, string sortDirection, ImageClickEventHandler ascClickEventHandler, ImageClickEventHandler descClickEventHandler, string ascImageFilePath, string descImageFilePage, string headerTextCssClass) { if (e.Row != null && e.Row.RowType == DataControlRowType.Header) { int i = 0; foreach (TableCell tc in e.Row.Cells) { if (tc.HasControls() || (tc.Text != string.Empty && tc.Text.Trim() != " ")) { Table table = new Table(), imageTable = new Table(); table.CellPadding = 0; table.CellSpacing = 0; table.Style.Add(HtmlTextWriterStyle.VerticalAlign, "middle"); table.Attributes.Add("summary", "Header cell structual table"); imageTable.CellSpacing = 0; imageTable.CellPadding = 0; imageTable.Attributes.Add("summary", "Sort images structual table"); table.BorderStyle = BorderStyle.None; imageTable.BorderStyle = BorderStyle.None; TableRow row = null, AscImageRow, DescImageRow; TableCell AscImageCell, DescImageCell, headerTextCell, imagesCell; AscImageRow = new TableRow(); AscImageRow.BackColor = Color.Transparent; AscImageCell = new TableCell(); AscImageCell.BorderWidth = new Unit(0); AscImageCell.Style.Add(HtmlTextWriterStyle.Padding, "0"); AscImageCell.Style.Add(HtmlTextWriterStyle.Margin, "0"); AscImageRow.Cells.Add(AscImageCell); DescImageRow = new TableRow(); DescImageRow.BackColor = Color.Transparent; DescImageCell = new TableCell(); DescImageCell.BorderWidth = new Unit(0); DescImageCell.Style.Add(HtmlTextWriterStyle.Padding, "0"); DescImageCell.Style.Add(HtmlTextWriterStyle.Margin, "0"); DescImageRow.Cells.Add(DescImageCell); imagesCell = new TableCell(); imagesCell.Width = new Unit(1); imagesCell.BorderWidth = new Unit(0); imagesCell.Style.Add(HtmlTextWriterStyle.Padding, "0"); imagesCell.Style.Add(HtmlTextWriterStyle.Margin, "0"); imagesCell.Controls.Add(imageTable); row = new TableRow(); row.BackColor = Color.Transparent; headerTextCell = new TableCell(); headerTextCell.BorderWidth = new Unit(0); headerTextCell.Style.Add(HtmlTextWriterStyle.Padding, "0"); headerTextCell.Style.Add(HtmlTextWriterStyle.Margin, "0"); headerTextCell.HorizontalAlign = HorizontalAlign.Left; Label HeaderLabel = new Label(); HeaderLabel.CssClass = headerTextCssClass; headerTextCell.Controls.Add(HeaderLabel); if (tc.HasControls()) { // search for the header link LinkButton lnk = tc.Controls[0] as LinkButton; if (lnk != null) { ImageButton imgBtnUp = new ImageButton(); ImageButton imgBtnDown = new ImageButton(); imgBtnUp.ID = "imgAsc" + i.ToString(); imgBtnDown.ID = "imgDesc" + i.ToString(); i++; imgBtnUp.Click += new ImageClickEventHandler(ascClickEventHandler); imgBtnDown.Click += new ImageClickEventHandler(descClickEventHandler); imgBtnUp.CommandName = lnk.CommandName; imgBtnDown.CommandName = lnk.CommandName; imgBtnUp.CommandArgument = lnk.CommandArgument; imgBtnDown.CommandArgument = lnk.CommandArgument; //--set the properties HeaderLabel.Text = lnk.Text.Trim(); imgBtnUp.ImageUrl = ascImageFilePath; imgBtnUp.AlternateText = "ascending"; imgBtnDown.ImageUrl = descImageFilePage; imgBtnDown.AlternateText = "descending"; //// checking if the header link is the user's choice if (sortExpression != lnk.CommandArgument || sortDirection == "DESC") { AscImageCell.Controls.Add(imgBtnUp); imageTable.Rows.Add(AscImageRow); } if (sortExpression != lnk.CommandArgument || sortDirection == "ASC") { DescImageCell.Controls.Add(imgBtnDown); imageTable.Rows.Add(DescImageRow); } //--this will remove the clickable column header link button that is automatically created //--when adding sorting capabilities to a gridview tc.Controls.RemoveAt(0); if (!AscImageCell.HasControls()) { AscImageCell.Dispose(); AscImageRow.Dispose(); } if (!DescImageCell.HasControls()) { DescImageCell.Dispose(); DescImageRow.Dispose(); } row.Cells.Add(imagesCell); row.Cells.Add(headerTextCell); table.Rows.Add(row); tc.Style.Add(HtmlTextWriterStyle.VerticalAlign, "middle"); tc.Controls.Add(table); } } else { imagesCell.Dispose(); HeaderLabel.Text = tc.Text; headerTextCell.VerticalAlign = VerticalAlign.Middle; row.Cells.Add(headerTextCell); table.Rows.Add(row); tc.Style.Add(HtmlTextWriterStyle.VerticalAlign, "middle"); tc.Controls.Add(table); } } } } }