0
我正在一個項目中,我需要通過代碼隱藏創建一個Datagrid TemplateColumn,它將包含一個ImageButton;我已經找到了需要的CodeProject我,但我唯一的問題是,當我運行在運行時代碼中,我得到試圖尋找通過數據網格ITemplate ID需要是唯一的
多的ItemDataBound方法控制時,出現以下異常找到具有相同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();
}