Title: ASP. In Network 2.0, dynamically adding template columns to the display data table (a control) and automatically binding data
终极管理员 知识笔记 95阅读
在孟子E章看了一下,但是他的数据不能自动绑定,需要在RowDataBind事件中处理。它被改进了。您可以指定要自动绑定的数据字段。对于文本框,在实例化: tb中添加一个事件。数据绑定=new EventHandler(TB _ data binding);然后在事件函数tb _数据中绑定: tb .Text=((DataRowView)容器。数据字段ToString();这样,地面可见性就可以自动绑定到他的数据源对应的数据表中的列数据字段对应的数据上公共类GridView template TextBox 3360 Item Plate { PrivateDataControlRowTypeTemplateType;privatestringcolumnnameprivatestringdatafieldpublicgridview模板textbox(DataControlRowTypetype,stringcolname,字符串数据字段)e;background-color: rgba(255,255,255,1)' { template type=type;column name=colnamedataField=data field } publicvindinstantiatein(System .{开关(模板类型)
ehighlighter1_462_977_Open_Text">{case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
TextBox tb = new TextBox();
tb.ID = container.ClientID;
tb.DataBinding += new EventHandler(tb_DataBinding);
container.Controls.Add(tb);
break;
default:
break;
}
}
private void tb_DataBinding(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
tb.Width = Unit.Percentage(100);
GridViewRow container = (GridViewRow)tb.NamingContainer;
tb.Text = ((DataRowView)container.DataItem)[dataField].ToString();
tb.Width = Unit.Pixel(70);
tb.Style.Add("TEXT-ALIGN", "right");
}
}

标签: