我正在开发一个项目,我需要通过代码隐藏创建一个Datagrid TemplateColumn,它将包含一个ImageButton;我在CodeProject找到了我需要的东西,但我唯一的问题是当我在运行时运行代码时,在尝试通过DataGrid的ItemDataBound方法找到Control时出现以下异常
具有相同ID的多个控件&uxPlusMinusImageButton'被找到。 FindControl要求控件具有唯一ID。
这是我的代码
DataGridPopulateMethod
private void PopulateDataGrid()
{
TemplateColumn plusMinusContractImage = new TemplateColumn();
plusMinusContractImage.ItemTemplate = new PlusMinusColumn("uxPlusMinusImageButton");
uxSummaryInfoDataGrid.ItemDataBound += new DataGridItemEventHandler(uxSummaryInfoDataGrid_ItemDataBound);
uxSummaryInfoDataGrid.DataSource = contracts;
uxSummaryInfoDataGrid.Columns.Add(new BoundColumn() { HeaderText = "ID", DataField = "ColumnID", Visible = false });
uxSummaryInfoDataGrid.Columns.Add(plusMinusContractImage);
uxSummaryInfoDataGrid.Columns.Add(new BoundColumn() { HeaderText = "Title 1", DataField = "Column1" });
uxSummaryInfoDataGrid.Columns.Add(new BoundColumn() { HeaderText = "Title 2", DataField = "Column2" });
uxSummaryInfoDataGrid.DataBind();
DataGridOnItemDataBoundMethod
private void uxSummaryInfoDataGridItemDataBound(DataGrid summaryDataGrid, DataGridItem item)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
SummaryInformation summaryInfo = (SummaryInformation)item.DataItem;
ImageButton plusMinusImageButton = (ImageButton)item.Cells[1].FindControl("uxPlusMinusImageButton");
plusMinusImageButton.Click += new ImageClickEventHandler(PlusMinusImageButton_Click);
ImageButton单击
protected void PlusMinusImageButton_Click(object sender, ImageClickEventArgs e)
{
PopulateDataGrid();
}
答案 0 :(得分:0)
我解决了这个问题,我收到了这个错误,因为我没有清除原来的动态datagrid列,因此它正在制作具有相同名称的控件。