Specifies the ItemTemplate of the GridView in the background

Source: Internet
Author: User

In fact, server controls are not recommended for the first project after work, but recently a company's old system is being maintained, using the GridView.

The new requirement is to add the sorting function on the GridView header. The original result is a pagination-free GridView. the scroll bar is displayed when the height is exceeded, So I roughly determined that the data volume here is not large. The implementation method is to directly use the HTML of the current GridView as the XML data source (double quotation marks of the attribute values in the HTML string obtained by innerHtml under IE do not exist, so this string will not be recognized as the standard XML, only the method that automatically adds double quotation marks on the Internet can be used as XML), and then sort using familiar XSLT. Not long after the results were released, the customer service told us that the sorting was slow and the tester was asked to know that there were more than 2000 pieces of data ......

Sping of this system. I was just getting started with the NET + NHibernate architecture. In addition, the business in the system is quite complicated, and I have been cautious when changing new functions. I have always maintained the principle of minimizing the changes to the original method. After learning about the new problem, I used AJAX to send the sorting request. After the background sorting, I bound the GridView and then got the HTML OF THE GridView and returned it to the foreground, the foreground uses JS to update the list.

In fact, the page has already added a GridView and bound data. However, during the actual test, the progress bar of the IE status bar has not been completed even after the sorting is completed. At that time, it was determined that a problem occurred when using JS to operate the Server Control GridView. Therefore, the front-end GridView was deleted. Instead, the GridView was declared in the background and initialized and bound. However, after the test, it is found that the progress bar is not completed. Using Fiddler monitoring, it is found that a csshover is requested every time the sorting is completed. htc file. Try to reference csshover. the style of the htc file is exactly the style of the GridView. After the style is removed, the progress bar does not finish.

The template class used to specify the ItemTemplate of the GridView in the background is as follows:

public class GridViewTextTemplate : ITemplate{    private string templateTypeName;    private string columnName;    private string Id;    public GridViewTextTemplate(string typeStr, string colname, string controlId)    {        templateTypeName = typeStr;        columnName = colname;        Id = controlId;    }    public void InstantiateIn(System.Web.UI.Control container)    {        switch (templateTypeName)        {            case "CheckBox":                CheckBox checkBox = new CheckBox();                checkBox.ID = Id;                container.Controls.Add(checkBox);                break;            case "Label":                Label label = new Label();                label.ID = Id;                label.DataBinding += new EventHandler(this.OnDataBinding);                container.Controls.Add(label);                break;            case "HiddenField&LableInPanel":                HiddenField hiddenField = new HiddenField();                hiddenField.ID = "RecordID";                hiddenField.DataBinding += new EventHandler(this.OnDataBinding);                container.Controls.Add(hiddenField);                Panel panel = new Panel();                panel.CssClass = "panleStyle";                Label labelInPanel = new Label();                labelInPanel.ID = Id;                labelInPanel.DataBinding += new EventHandler(this.OnDataBinding);                panel.Controls.Add(labelInPanel);                container.Controls.Add(panel);                break;            default:                break;        }    }    private void OnDataBinding(Object sender, EventArgs e)    {        GridViewRow row;        if (columnName != "")        {            switch (templateTypeName)            {                case "Label":                    Label labelInPanel = (Label)sender;                    row = (GridViewRow)labelInPanel.NamingContainer;                    labelInPanel.Text = Convert.ToDateTime(DataBinder.Eval(row.DataItem, columnName)).ToString("yyyy-MM-dd");                    break;                case "HiddenField&LableInPanel":                    if (sender is HiddenField)                    {                        HiddenField hiddenField = (HiddenField)sender;                        row = (GridViewRow)hiddenField.NamingContainer;                        hiddenField.Value = DataBinder.Eval(row.DataItem, "ID").ToString();                        break;                    }                    else if (sender is Label)                    {                        Label label = (Label)sender;                        row = (GridViewRow)label.NamingContainer;                        label.Text = DataBinder.Eval(row.DataItem, "RecordNo").ToString().Length > 18 ? DataBinder.Eval(row.DataItem, "RecordNo").ToString().Substring(0, 18) + "..." : DataBinder.Eval(row.DataItem, "RecordNo").ToString();                    }                    break;                default:                    break;            }        }    }}

Usage:

Private GridView GetRecordGridView () {GridView grid_Record = new GridView (); grid_Record.ID = "grid_Record"; grid_Record.Width = 260; rows = false; grid_Record.DataKeyNames = new string [] {"Id "}; topology = "listGridView"; TemplateField col_chose = new TemplateField (); col_chose.HeaderText = ""; col_chose.ItemTemplate = new GridViewTextTemplate ("CheckBox", "", "chkRecord "); values (col_chose); TemplateField col_recordID = new TemplateField (); numbers = "ticket number"; // col_recordID.ItemTemplate = new GridViewTextTemplate ("HiddenField", "ID", "RecordID "); updated = new GridViewTextTemplate ("HiddenField & LableInPanel", "RecordNo", "success"); updated (col_recordID); TemplateField col_saveDate = new TemplateField (); Updated = "date "; col_saveDate.ItemTemplate = new GridViewTextTemplate ("Label", "SaveDate", "lblSaveDate"); grid_Record.Columns.Add (col_saveDate); return grid_Record ;}

How to obtain the html of the GridView:

    public string RenderGridView(GridView grid)    {        System.Text.StringBuilder sb = new System.Text.StringBuilder();        System.IO.StringWriter sw = new System.IO.StringWriter(sb);        HtmlTextWriter htw = new HtmlTextWriter(sw);        grid.RenderControl(htw);        return sb.ToString();    }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.