螞蟻空間

螞蟻空間

导航

将 TemplateField 字段列动态添加到 GridView 控件

<%@ Page language="C#" %>

 

<script runat="server">

 

    // Create a dynamic template column

    public class GridViewTextTemplate : System.Web.UI.ITemplate

    {

        private DataControlRowType templateType;

        private string columnName;

        private string cId;

 

        public GridViewTextTemplate(DataControlRowType type, string colname, string controlId)

        {

            templateType = type;

            columnName = colname;

            cId = controlId;

        }

 

        public void InstantiateIn(System.Web.UI.Control container)

        {

            // Create the content for the different row types.

            switch (templateType)

            {

                case DataControlRowType.Header:

                    // Create the controls and set id properties to put in the header

                    myHeadLiteral.ID = cId;

                    myHeadLiteral.Text = "<B>" + columnName + "</B>";

 

                    container.Controls.Add(myHeadLiteral);

                    break;

                case DataControlRowType.DataRow:

                    // Create the controls and set id properties to put in a data row

                    TextBox myTextBox = new TextBox();

                    myTextBox.ID = cId;

                    myTextBox.DataBinding += new EventHandler(this.TextBoxDataBinding);

 

                    container.Controls.Add(myTextBox);

                    break;

                default:

                    // Insert code to handle unexpected values.

                    break;

            }

        }

 

        private void TextBoxDataBinding(Object sender, EventArgs e)

        {

            TextBox myTextBox = (TextBox)sender;

            GridViewRow row = (GridViewRow)myTextBox.NamingContainer;

 

            myTextBox.Text = System.Web.UI.DataBinder.Eval(row.DataItem, columnName).ToString();

        }

    }

 

 

    void Page_Load(Object sender, EventArgs e)

    {

 

        // Create the field columns when the page is first loaded.

        if (!IsPostBack)

        {

            TemplateField tField;

 

            // Create the dynamic templates

            tField = new TemplateField();

            tField.ItemTemplate = new GridViewTextTemplate(DataControlRowType.DataRow, "parameter_description", "textParameterDesc");

            tField.HeaderTemplate = new GridViewTextTemplate(DataControlRowType.Header, "说明", "textParameterDescHd");

 

            // Add the dynamic templates field column to the GridView

            GridView1.Columns.Add(tField);

        }

    }

 

</script>

 

<html>

    <body>

        <form id="Form1" runat="server">

 

            <asp:gridview id="GridView1"

                datasourceid="TestSqlDataSource"

                autogeneratecolumns="False"

                runat="server">                

            </asp:gridview>

               

            <asp:sqldatasource id="TestSqlDataSource" 

                selectcommand="Select * From Parameter_Lines"

                connectionstring="server=localhost;database=Test;integrated security=SSPI"

                runat="server">

            </asp:sqldatasource>

               

        </form>

    </body>

</html>

posted on 2006-09-15 12:08  螞蟻  阅读(5568)  评论(5编辑  收藏  举报