带有自定义 ItemTemplate 的 ASP.Net 动态 Gridview
我需要一些帮助来渲染动态网格视图。 使用下面的源代码,我可以将网格添加到页面,并为数据源中的每个条目生成一行。 问题是控件仅出现在最后一行。如果我向数据源添加另一个条目,我会在 Gridview 中多显示一行,但只有最后一行显示我使用 ItemTemplate 添加的控件。 当我调试解决方案时,我可以看到它为每个列实例化了一个新的 ItemTemplate 并添加了所有控件,但在网格数据绑定之后,所有单元格都是空的,除了最后一行的单元格。
如果可以的话请帮忙。谢谢
private void BuildGridInterface()
{
TBL_PARAM_01 xpto = new TBL_PARAM_01();
GridView grid = new GridView();
grid.ID = "grd";
grid.AutoGenerateColumns = false;
grid.ShowHeader = true;
PropertyInfo[] props = xpto.GetType().GetProperties();
foreach (PropertyInfo info in props)
{
TemplateField field = new TemplateField();
object[] attributes = info.GetCustomAttributes(true);
var att = attributes.SingleOrDefault(a => a.GetType() == typeof(Domain));
WebControl control;
if (att != null)
{
control = new DropDownList();
control.ID = "drp" + info.Name;
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXXX", Value = "1" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX2", Value = "2" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX3", Value = "3" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX4", Value = "4" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX5", Value = "5" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX6", Value = "6" });
}
else
{
control = new TextBox();
control.ID = "txt" + info.Name;
}
field.ItemTemplate = new ItemTemplate(control, false, info.Name);
field.HeaderText = ((MatrixFieldLabel)attributes.Single(a => a.GetType() == typeof(MatrixFieldLabel))).Value;
grid.Columns.Add(field);
}
FillGrid(grid);
placer.Controls.Add(grid);
}
public class ItemTemplate : ITemplate
{
WebControl Control { get; set; }
bool Enabled { get; set; }
string ColumName { get; set; }
public ItemTemplate(WebControl control, bool enabled, string columname)
{
this.Control = control;
this.Enabled = enabled;
this.ColumName = columname;
control.DataBinding += new EventHandler(Control_DataBinding);
}
public void InstantiateIn(Control container)
{
((WebControl)container).Enabled = Enabled;
container.Controls.Add(this.Control);
}
void Control_DataBinding(object sender, EventArgs e)
{
GridViewRow container = (GridViewRow)((Control)sender).NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, this.ColumName);
if (sender.GetType() == typeof(TextBox))
((TextBox)sender).Text = dataValue.ToString();
else if (sender.GetType() == typeof(DropDownList))
((DropDownList)sender).SelectedValue = dataValue.ToString();
}
private void FillGrid(GridView target)
{
List<TBL_PARAM_01> list = new List<TBL_PARAM_01>();
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 1, IDEntityRecordX = 2, Output = "XPTO 3" });
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 2, IDEntityRecordX = 3, Output = "XPTO 4" });
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 3, IDEntityRecordX = 4, Output = "XPTO 5" });
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 4, IDEntityRecordX = 5, Output = "XPTO 6" });
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 5, IDEntityRecordX = 6, Output = "XPTO 7" });
target.DataSource = list;
target.DataBind();
}
}
I need some help in rendering a dynamic gridview.
Using the source code below, I can add the grid to the page and it generates one row for each entry in the datasource.
The problem is that the controls only appears in the last row. If I add another entry to the datasource, I get one more row in the Gridview but only the last is showing the controls that I've added using the ItemTemplate.
When I debug the solution I can see that it instatiates a new ItemTemplate foreach collumn and that it adds all the controls, but after the databinding of the grid, all the cells are empty, except the ones of the last row.
Please help if you can. Thanks
private void BuildGridInterface()
{
TBL_PARAM_01 xpto = new TBL_PARAM_01();
GridView grid = new GridView();
grid.ID = "grd";
grid.AutoGenerateColumns = false;
grid.ShowHeader = true;
PropertyInfo[] props = xpto.GetType().GetProperties();
foreach (PropertyInfo info in props)
{
TemplateField field = new TemplateField();
object[] attributes = info.GetCustomAttributes(true);
var att = attributes.SingleOrDefault(a => a.GetType() == typeof(Domain));
WebControl control;
if (att != null)
{
control = new DropDownList();
control.ID = "drp" + info.Name;
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXXX", Value = "1" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX2", Value = "2" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX3", Value = "3" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX4", Value = "4" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX5", Value = "5" });
((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX6", Value = "6" });
}
else
{
control = new TextBox();
control.ID = "txt" + info.Name;
}
field.ItemTemplate = new ItemTemplate(control, false, info.Name);
field.HeaderText = ((MatrixFieldLabel)attributes.Single(a => a.GetType() == typeof(MatrixFieldLabel))).Value;
grid.Columns.Add(field);
}
FillGrid(grid);
placer.Controls.Add(grid);
}
public class ItemTemplate : ITemplate
{
WebControl Control { get; set; }
bool Enabled { get; set; }
string ColumName { get; set; }
public ItemTemplate(WebControl control, bool enabled, string columname)
{
this.Control = control;
this.Enabled = enabled;
this.ColumName = columname;
control.DataBinding += new EventHandler(Control_DataBinding);
}
public void InstantiateIn(Control container)
{
((WebControl)container).Enabled = Enabled;
container.Controls.Add(this.Control);
}
void Control_DataBinding(object sender, EventArgs e)
{
GridViewRow container = (GridViewRow)((Control)sender).NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, this.ColumName);
if (sender.GetType() == typeof(TextBox))
((TextBox)sender).Text = dataValue.ToString();
else if (sender.GetType() == typeof(DropDownList))
((DropDownList)sender).SelectedValue = dataValue.ToString();
}
private void FillGrid(GridView target)
{
List<TBL_PARAM_01> list = new List<TBL_PARAM_01>();
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 1, IDEntityRecordX = 2, Output = "XPTO 3" });
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 2, IDEntityRecordX = 3, Output = "XPTO 4" });
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 3, IDEntityRecordX = 4, Output = "XPTO 5" });
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 4, IDEntityRecordX = 5, Output = "XPTO 6" });
list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 5, IDEntityRecordX = 6, Output = "XPTO 7" });
target.DataSource = list;
target.DataBind();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题解决了。我必须重写 ItemTemplate 类的 InstantiateIn 方法。
谢谢。
Problem solved. I've had to rewrite the method InstantiateIn of my ItemTemplate class.
Thanks.
每次调用 FillGrid 时,您似乎都会替换目标 gridview 的数据源,并重新绑定它。您可以删除 FillGrid 方法之外以及 FillGrid(grid); 之间的 DataSource 分配和 DataBind 调用;和 placer.Controls.Add(grid); BuildGridInterface 方法中的行?
让我们知道这是否有帮助..:)
you appear to be replacing the datasource for your target gridview every time you call FillGrid, and rebinding it. You could remove your DataSource assignment and DataBind calls outside of the FillGrid method and between FillGrid(grid); and placer.Controls.Add(grid); lines in the BuildGridInterface method?
Let us know if that helps.. :)