This is a migrated thread and some comments may be shown as answers.

Create GridEditCommandColumn in codebehind

8 Answers 423 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SonicImaging
Top achievements
Rank 1
SonicImaging asked on 28 Dec 2008, 04:13 PM
Hi,  im creating a GridEditCommandColumn in the Page_Init and I have AutoGenerateEditColumn = false set for my grid.   When I run the page the edit command shows but when I hit edit I get the following error.  The rad grid tag is in the aspx page so im not creating the grid programiclly just the columns and settings the values for the radgrid.

[HttpException (0x80004005): Multiple controls with the same ID 'EditButton' were found. FindControl requires that controls have unique IDs.]
   System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +273
   System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +320
   System.Web.UI.Control.EnsureNamedControlsTable() +61
   System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +222
   System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +327
   System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +327
   System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +327
   System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +327
   System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +327
   System.Web.UI.Page.FindControl(String id) +38
   System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +629
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1743

Here is the code.  If I remove this and set AutoGenerateEditColumn to true it works.

       GridEditCommandColumn editCommandColumn = new GridEditCommandColumn(); 
        RG1.MasterTableView.Columns.Add(editCommandColumn); 
        editCommandColumn.HeaderText = ""
        editCommandColumn.UniqueName = "EditCol"
        editCommandColumn.Display = true
        editCommandColumn.HeaderStyle.Width = Unit.Pixel(30); 
        editCommandColumn.ItemStyle.Width = Unit.Pixel(30); 
        editCommandColumn.FooterStyle.Width = Unit.Pixel(30); 
        editCommandColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center; 
        editCommandColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; 

Why am i getting this error when the grid goes into edit mode.

Thanks in advance
-Keith


8 Answers, 1 is accepted

Sort by
0
SonicImaging
Top achievements
Rank 1
answered on 29 Dec 2008, 12:11 AM
I fixed my problem with checking to see if (!IsPostBack),  but I didn't think Page_Init gets called during a post back only Page_Load.

Is it supposed to call Page_Init on postback?

Thanks
-Keith
0
Vlad
Telerik team
answered on 29 Dec 2008, 08:29 AM
Hello Keith,

The entire page lifecycle will be executed in both cases - initial load and post-back.

Greetings,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
SonicImaging
Top achievements
Rank 1
answered on 29 Dec 2008, 01:26 PM
Thanks,  The example in the docs should be changed then since in Page_Init example it doesn't show checking for postback only for page_load
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

Then also mentioned here,  to only check for postback on page_load
http://www.telerik.com/help/aspnet-ajax/grdusingcolumns.html

Also the demo doesn't do this
http://demos.telerik.com/aspnet-ajax/Grid/Examples/Hierarchy/DefiningStructure/DefaultCS.aspx

The only difference with my code is that im defining the grid itself in the aspx page then setting the grid values and adding the columns in the page_init since im also adding template columns which have to be created in the page_init.  I wanted to put all the code for creating the grid in page_init so its in one place.  is this the reason its being called on post back?  where the example in page_init also creates the grid in code not in the aspx

Also is it still true here http://www.telerik.com/help/aspnet-ajax/grdusingcolumns.html

When adding columns programmatically, you must add the created columns to the Columns collection at a different point, depending on whether you are instantiating the column on PageInit or PageLoad.

When adding columns using on PageInit, you must add the columns to the Columns collection after their properties have been set:


Thanks
-Keith





0
SonicImaging
Top achievements
Rank 1
answered on 30 Dec 2008, 04:58 PM
Hi,  Ive sorted out my issues and now Page_Init isn't being called on post back and everything is working properly with created the grid in codebehind

In you other post you mentioned there are times Page_Init will be called on post back.  do you know when or what would cause that to happen?  Is that just with RadGrid?

Thanks
-Keith
0
Laurel
Top achievements
Rank 1
answered on 13 Dec 2011, 04:18 PM
I am having the same problem as the person in the original post (Sys.WebForms.PageRequestManagerServerErrorException: Multiple controls with the same ID 'EditButton' were found. FindControl requires that controls have unique IDs.).  My grid is defined in my aspx file but my columns are defined in the form Init.  I add the columns to the grid after all columns are created and all properties are set.  

Is there a working sample with this same scenario?

Here's my code:
aspx code:
..
<telerik:RadGrid ID="gridMeasures" runat="server" >
<MasterTableView 
DataKeyNames="m_iWAPMeasureID" >
</MasterTableView> 
</telerik:RadGrid>



vb code:
Protected Sub form1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Init

gridMeasures.AllowPaging = True
gridMeasures.AutoGenerateColumns = True
gridMeasures.ShowStatusBar = True
gridMeasures.GridLines = GridLines.None
gridMeasures.Skin = "Hay"
gridMeasures.ClientSettings.Scrolling.AllowScroll = True
gridMeasures.ClientSettings.Scrolling.UseStaticHeaders = True
gridMeasures.PageSize = 50
gridMeasures.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top
gridMeasures.MasterTableView.EditFormSettings.EditColumn.Display = True
gridMeasures.MasterTableView.EditMode = GridEditMode.InPlace

AddHandler gridMeasures.DeleteCommand, AddressOf gridMeasures_DeleteCommand
AddHandler gridMeasures.NeedDataSource, AddressOf gridMeasures_NeedDataSource
AddHandler gridMeasures.InsertCommand, AddressOf gridMeasures_InsertCommand
AddHandler gridMeasures.PreRender, AddressOf gridMeasures_PreRender
AddHandler gridMeasures.UpdateCommand, AddressOf gridMeasures_UpdateCommand


Dim oWAPMeasureIDColumn As New GridBoundColumn
oWAPMeasureIDColumn.UniqueName = "m_iWAPMeasureID"
oWAPMeasureIDColumn.DataField = "m_iWAPMeasureID"
oWAPMeasureIDColumn.Visible = False


Dim oEditColumn As New GridEditCommandColumn
oEditColumn.UniqueName = "EditColumn"


Dim oDeletedColumn As New GridButtonColumn
oDeletedColumn.ConfirmText = "Delete this Measure?"
oDeletedColumn.ConfirmDialogType = GridConfirmDialogType.RadWindow
oDeletedColumn.ConfirmTitle = "Delete"
oDeletedColumn.ButtonType = GridButtonColumnType.ImageButton
oDeletedColumn.CommandName = "Delete"
oDeletedColumn.Text = "Delete"
oDeletedColumn.UniqueName = "DeleteColumn"
oDeletedColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center
oDeletedColumn.ItemStyle.CssClass = "MyImageButton"


Dim oSelectedColumn As New GridTemplateColumn
oSelectedColumn.HeaderText = String.Empty
oSelectedColumn.ItemTemplate = New clsItemSelectedTemplate("m_bSelected")
oSelectedColumn.EditItemTemplate = New clsEditSelectedTemplate("m_bSelected")
oSelectedColumn.DataField = "m_bSelected"
oSelectedColumn.UniqueName = "m_bSelected"


Dim oCategoryColumn As New GridTemplateColumn()
oCategoryColumn.ItemTemplate = New clsMeasureCategoryItemTemplate("m_sMeasureCategory")
oCategoryColumn.EditItemTemplate = New clsMeasureCategoryEditTemplate("m_sMeasureCategory", sAgId)
oCategoryColumn.DataField = "m_sMeasureCategory"
oCategoryColumn.UniqueName = "m_sMeasureCategory"
oCategoryColumn.HeaderText = "Measure Category"


Dim oDescriptionColumn As New GridTemplateColumn()
oDescriptionColumn.ItemTemplate = New clsMeasureDescriptionItemTemplate("m_sMeasureDescription")
oDescriptionColumn.EditItemTemplate = New clsMeasureDescriptionEditTemplate("m_sMeasureDescription", sAgId)
oDescriptionColumn.DataField = "m_sMeasureDescription"
oDescriptionColumn.UniqueName = "m_sMeasureDescription"
oDescriptionColumn.HeaderText = "Measure Description"


Dim oPricePerUnitColumn As New GridTemplateColumn()
oPricePerUnitColumn.ItemTemplate = New clsPricePerUnitItemTemplate("m_decPricePerUnit")
oPricePerUnitColumn.EditItemTemplate = New clsPricePerUnitEditTemplate("m_decPricePerUnit")
oPricePerUnitColumn.DataField = "m_decPricePerUnit"
oPricePerUnitColumn.UniqueName = "m_decPricePerUnit"
oPricePerUnitColumn.HeaderText = "Unit Price"
oPricePerUnitColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right


Dim oUnitColumn As New GridTemplateColumn()
oUnitColumn.ItemTemplate = New clm_sUnitItemTemplate("m_sUnit")
oUnitColumn.EditItemTemplate = New clm_sUnitEditTemplate("m_sUnit")
oUnitColumn.DataField = "m_sUnit"
oUnitColumn.UniqueName = "m_sUnit"
oUnitColumn.HeaderText = "Unit"


Dim oAuthQtyColumn As New GridTemplateColumn()
oAuthQtyColumn.ItemTemplate = New clsAuthQtyItemTemplate("m_decAuthorizedQty")
oAuthQtyColumn.EditItemTemplate = New clsAuthQtyEditTemplate("m_decAuthorizedQty")
oAuthQtyColumn.HeaderText = "Authorized Qty"
oAuthQtyColumn.UniqueName = "m_decAuthorizedQty"
oAuthQtyColumn.DataField = "m_decAuthorizedQty"
oAuthQtyColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right
oAuthQtyColumn.ItemStyle.Width = 65


Dim oActualQtyColumn As New GridTemplateColumn()
oActualQtyColumn.ItemTemplate = New clsActQtyItemTemplate("m_decQuantity")
oActualQtyColumn.EditItemTemplate = New clsActQtyEditTemplate("m_decQuantity")
oActualQtyColumn.HeaderText = "Actual Qty"
oActualQtyColumn.UniqueName = "m_decQuantity"
oActualQtyColumn.DataField = "m_decQuantity"
oActualQtyColumn.ItemStyle.Wrap = True
oActualQtyColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right
oActualQtyColumn.ItemStyle.Width = 65


Dim oTotalPriceColumn As New GridTemplateColumn()
oTotalPriceColumn.ItemTemplate = New clsTotalPriceItemTemplate("m_decTotalPrice")
oTotalPriceColumn.EditItemTemplate = New clsTotalPriceEditTemplate("m_decTotalPrice")
oTotalPriceColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right
oTotalPriceColumn.DataField = "m_decTotalPrice"
oTotalPriceColumn.UniqueName = "m_decTotalPrice"
oTotalPriceColumn.HeaderText = "Total Price"


Dim oStatusColumn As New GridTemplateColumn()
oStatusColumn.ItemTemplate = New clm_sStatusItemTemplate("m_sStatus")
oStatusColumn.EditItemTemplate = New clm_sStatusEditTemplate("m_sStatus")
oStatusColumn.DataField = "m_sStatus"
oStatusColumn.UniqueName = "m_sStatus"
oStatusColumn.HeaderText = "Status"


Dim oContractorColumn As New GridTemplateColumn()
oContractorColumn.ItemTemplate = New clsContractorItemTemplate("m_sContractor")
oContractorColumn.EditItemTemplate = New clsContractorEditTemplate("m_sContractor", sAgId)
oContractorColumn.DataField = "m_sContractor"
oContractorColumn.UniqueName = "m_sContractor"
oContractorColumn.HeaderText = "Contractor"


Dim oFundSourceColumn As New GridTemplateColumn()
oFundSourceColumn.ItemTemplate = New clsPrimaryFundSourceItemTemplate("m_sPrimaryFundSource")
oFundSourceColumn.EditItemTemplate = New clsPrimaryFundSourceEditTemplate("m_sPrimaryFundSource", sAgId)
oFundSourceColumn.DataField = "m_sPrimaryFundSource"
oFundSourceColumn.UniqueName = "m_sPrimaryFundSource"
oFundSourceColumn.HeaderText = "Funding Source"


Dim oStateCategoryColumn As New GridTemplateColumn()
oStateCategoryColumn.ItemTemplate = New clm_sStateCategoryItemTemplate("m_sStateCategory")
oStateCategoryColumn.EditItemTemplate = New clm_sStateCategoryEditTemplate("m_sStateCategory", sAgId)
oStateCategoryColumn.DataField = "m_sStateCategory"
oStateCategoryColumn.UniqueName = "m_sStateCategory"
oStateCategoryColumn.HeaderText = "BWR Category"


Dim oMeasureInfoID As New GridBoundColumn
oMeasureInfoID.UniqueName = "m_iMeasureInfoID"
oMeasureInfoID.DataField = "m_iMeasureInfoID"
oMeasureInfoID.Visible = False


'Add the columns to the grid
gridMeasures.MasterTableView.Columns.Add(oWAPMeasureIDColumn)
gridMeasures.MasterTableView.Columns.Add(oEditColumn)
gridMeasures.MasterTableView.Columns.Add(oDeletedColumn)
gridMeasures.MasterTableView.Columns.Add(oSelectedColumn)
gridMeasures.MasterTableView.Columns.Add(oCategoryColumn)
gridMeasures.MasterTableView.Columns.Add(oDescriptionColumn)
gridMeasures.MasterTableView.Columns.Add(oPricePerUnitColumn)
gridMeasures.MasterTableView.Columns.Add(oUnitColumn)
gridMeasures.MasterTableView.Columns.Add(oAuthQtyColumn)
gridMeasures.MasterTableView.Columns.Add(oActualQtyColumn)
gridMeasures.MasterTableView.Columns.Add(oTotalPriceColumn)
gridMeasures.MasterTableView.Columns.Add(oStatusColumn)
gridMeasures.MasterTableView.Columns.Add(oContractorColumn)
gridMeasures.MasterTableView.Columns.Add(oFundSourceColumn)
gridMeasures.MasterTableView.Columns.Add(oStateCategoryColumn)
gridMeasures.MasterTableView.Columns.Add(oMeasureInfoID)


End Sub
0
Tsvetina
Telerik team
answered on 16 Dec 2011, 03:52 PM
Hello Laurel,

Please, note that when you have declared your grid in the mark-up, you should add the columns on initial Page_Load in order to avoid this issue. Try moving the column adding logic to the Page_Load event following the sample code provided in this help article:
Dynamically defining the structure of a statically-declared grid

Best wishes,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Laurel
Top achievements
Rank 1
answered on 21 Dec 2011, 05:08 PM
Moving the column creation to Page Load could be a problem given the restriction about creating template columns which I use.  I found this caution in your documentation:

Creating columns in the Page_Load event handler does not work for template columns. For the controls inside a template to persist their ViewState, the grid must be generated completely in the code-behind using the Page_Init event (see below). That way, template controls are instantiated before the LoadViewState event of the page.


Is this still a restriction, and if so, is there a work-around?
0
Tsvetina
Telerik team
answered on 22 Dec 2011, 01:04 PM
Hello Laurel,

I missed out the template columns, indeed Page_Init is the correct place to create them. However, also note that the same sentence states that the grid itself must be generated in Page_Init, too. So, in order to get correct grid behavior, you need to move the RadGrid declaration in code-behind, too.
This:
<telerik:RadGrid ID="gridMeasures" runat="server" >
    <MasterTableView
        DataKeyNames="m_iWAPMeasureID" >
    </MasterTableView>
</telerik:RadGrid>

should be converted to this and added in the beginning of Page_Init event:
Dim gridMeasures As New RadGrid()
gridMeasures.ID = "gridMeasures"
gridMeasures.DataKeyNames = New String() {"m_iWAPMeasureID"}


Greetings,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
SonicImaging
Top achievements
Rank 1
Answers by
SonicImaging
Top achievements
Rank 1
Vlad
Telerik team
Laurel
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or