GridViewTextTemplate類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;/// <summary>
///GridViewTextTemplate 的摘要說明
/// </summary>
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 headerLiteral myHeadLiteral = new Literal();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 rowTextBox myTextBox = new TextBox();myTextBox.ID = cId;myTextBox.DataBinding += new EventHandler(this.TextBoxDataBinding);myTextBox.Width = 100;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();}}
在后臺GridView動態添加TemplateField:
//parameter_description綁定的數據 textParameterDesc是Text的IDTemplateField tField;tField = new TemplateField();tField.HeaderTemplate = new GridViewTextTemplate(DataControlRowType.Header, "說明", "textParameterDescHd");tField.ItemTemplate = new GridViewTextTemplate(DataControlRowType.DataRow, "parameter_description", "textParameterDesc");GridView1.Columns.Add(tField);
或者:
GridView1.Columns.Add(new TemplateField() { HeaderTemplate = new GridViewTextTemplate(DataControlRowType.Header, "序號", ""), ItemTemplate = new GridViewTextTemplate(DataControlRowType.DataRow, "PlanSN", "") });
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
配置如下
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态