ASP.NET2.0中给GridView动态添加模板列并自动绑定数据
2009-08-28 17:08
701 查看
转/article/5596953.html
在孟子E章上看了一下,但是他的数据是不能自动绑定上去的,需要再RowDataBind事件里面处理。
改进了一下。可以指定datafield,让其自动绑定上去。
对于TextBox在InstantiateIn中增加事件:
tb.DataBinding += new EventHandler(tb_DataBinding);
然后在事件函数tb_DataBinding里面:
tb.Text = ((DataRowView)container.DataItem)[dataField].ToString();
这样GV就可以自动绑定到他的DataSource对应的DataTable中列dataField对应的数据了。
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string dataField;
public GridViewTemplate(DataControlRowType type, string colname, string datafield)
{
templateType = type;
columnName = colname;
dataField = datafield;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
Label lbl = new Label();
lbl.ID = container.ClientID;
lbl.DataBinding += new EventHandler(lbl_DataBinding);
container.Controls.Add(lbl);
break;
default:
break;
}
}
private void lbl_DataBinding(object sender, EventArgs e)
{
Label lbl = (Label)sender;
lbl.Width = Unit.Percentage(100);
GridViewRow container = (GridViewRow)lbl.NamingContainer;
lbl.Text = ((DataRowView)container.DataItem)[dataField].ToString();
lbl.Width = Unit.Pixel(70);
lbl.Style.Add("TEXT-ALIGN", "right");
}
}
在孟子E章上看了一下,但是他的数据是不能自动绑定上去的,需要再RowDataBind事件里面处理。
改进了一下。可以指定datafield,让其自动绑定上去。
对于TextBox在InstantiateIn中增加事件:
tb.DataBinding += new EventHandler(tb_DataBinding);
然后在事件函数tb_DataBinding里面:
tb.Text = ((DataRowView)container.DataItem)[dataField].ToString();
这样GV就可以自动绑定到他的DataSource对应的DataTable中列dataField对应的数据了。
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string dataField;
public GridViewTemplate(DataControlRowType type, string colname, string datafield)
{
templateType = type;
columnName = colname;
dataField = datafield;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
Label lbl = new Label();
lbl.ID = container.ClientID;
lbl.DataBinding += new EventHandler(lbl_DataBinding);
container.Controls.Add(lbl);
break;
default:
break;
}
}
private void lbl_DataBinding(object sender, EventArgs e)
{
Label lbl = (Label)sender;
lbl.Width = Unit.Percentage(100);
GridViewRow container = (GridViewRow)lbl.NamingContainer;
lbl.Text = ((DataRowView)container.DataItem)[dataField].ToString();
lbl.Width = Unit.Pixel(70);
lbl.Style.Add("TEXT-ALIGN", "right");
}
}
相关文章推荐
- ASP.NET2.0中给GridView动态添加模板列并自动绑定数据
- ASP.NET2.0中给GridView动态添加模板列并自动绑定数据
- GridView动态添加模板列(并为其添加事件和自动绑定数据)
- GridView动态添加模板列并自动绑定数据
- GridView 动态添加模板列并绑定数据 实现从外部直接传入控件 和 绑定字段参数
- GridView动态添加模板列(数据绑定)
- GridView中给DropDownList动态绑定数据,及选择列表值后自动更新数据库
- 动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- 动态添加gridView 并绑定数据
- GridView 动态添加绑定列和模板列
- 如何在ASPXGridView绑定数据不满一页时自动添加空行
- 动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- GridView动态添加模板列,并解决数据列PostBack后数据丢失问题!
- Silverlight动态添加数据模板(以RadGridView为例子)
- 如何在ASPXGridView绑定数据不满一页时自动添加空行
- ASP.NET2.0中动态添加GridView模板列
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- GridView动态添加列并判断绑定数据DataTable的列类型控制展示内容
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)