简体   繁体   English

ITemplate ID必须是唯一的

[英]ITemplate ID needs to be unique

I am working on a project where i need to create a Datagrid TemplateColumn via the codebehind which will contain an ImageButton; 我正在一个项目中,我需要通过背后的代码创建一个Datagrid TemplateColumn,其中将包含一个ImageButton; i have found what i required on CodeProject , but the only issue i have is when i run the code at run-time i get the following exception when trying to find the Control via the ItemDataBound method of the DataGrid 我已经在CodeProject上找到了我需要的东西,但是唯一的问题是我在运行时运行代码时,尝试通过DataGrid的ItemDataBound方法查找控件时遇到以下异常

Multiple controls with the same ID 'uxPlusMinusImageButton' were found. 找到具有相同ID“ uxPlusMinusImageButton”的多个控件。 FindControl requires that controls have unique IDs. FindControl要求控件具有唯一的ID。

This is my code 这是我的代码

DataGridPopulateMethod 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 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 Click ImageButton请点击

protected void PlusMinusImageButton_Click(object sender, ImageClickEventArgs e)
{
  PopulateDataGrid();
}

我解决了这个问题,因为没有清除原始动态datagrid列,所以出现了此错误,因此它使控件具有相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM