日期:2014-05-18 浏览次数:20488 次
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
GridView1.Columns.Clear();
tf4 = new TemplateField();
tf4.ItemTemplate = new GridViewTemplate("Label", "计算机");
tf4.EditItemTemplate = new GridViewTemplate("TextBox", "计算机");
tf4.HeaderText = "计算机";
GridView1.Columns.Add(tf4);
tf4 = new TemplateField();
tf4.ItemTemplate = new GridViewTemplate("Label", "数学");
tf4.EditItemTemplate = new GridViewTemplate("TextBox", "数学");
tf4.HeaderText = "数学";
GridView1.Columns.Add(tf4);
cf = new CommandField();
cf.ShowDeleteButton = false;
cf.ShowCancelButton = false;
cf.ShowEditButton = true;
GridView1.Columns.Add(cf);
databind();
Label l = (Label)GridView1.Rows[3].FindControl("label计算机");
if (l != null) TextBox1.Text = "GridView1.Rows[3] label.text = " + l.Text;
Label l2 = (Label)GridView1.Rows[3].FindControl("labeltext");
if (l2 != null) TextBox2.Text = "GridView1.Rows[3] l2.text" + l2.Text;
//}
}
public class GridViewTemplate : ITemplate
{
private string temptype;
private string columnName;
public GridViewTemplate(string type, string colname)
{
temptype = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (temptype.ToLower())
{
case "textbox":
TextBox textbox = new TextBox();
textbox.ID = "textbox" +columnName;
textbox.Text = "test";
//container.Controls.Add(textbox);
textbox.DataBinding += new EventHandler(this.OnDataBinding);
textbox.TextChanged += new EventHandler(this.OnTextChanged);
container.Controls.Add(textbox);
break;
case "labe