Hi,
I have a custom column (who's inheriting from GridBoundColumn) who display an image related to the DataField. I override the method InitializeCell :
when the page load for the first time, the parameter inItem.DataItem is not null and contains the object representing a row from the DataSource.
On the other side, in the PostBack, inItem.DataItem is null ! So I can't retrieve my data...
Is something wrong ?
Thank you !
I have a custom column (who's inheriting from GridBoundColumn) who display an image related to the DataField. I override the method InitializeCell :
public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem)when the page load for the first time, the parameter inItem.DataItem is not null and contains the object representing a row from the DataSource.
On the other side, in the PostBack, inItem.DataItem is null ! So I can't retrieve my data...
Is something wrong ?
Thank you !
4 Answers, 1 is accepted
0
Hello Francis,
Based on the provided information it is hard to say what is causing the described issue on your side. Could you please post your aspx markup code with the related code behind file in order to provide you more to the point answer.
All the best,
Kostadin
the Telerik team
Based on the provided information it is hard to say what is causing the described issue on your side. Could you please post your aspx markup code with the related code behind file in order to provide you more to the point answer.
All the best,
Kostadin
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
Francis
Top achievements
Rank 1
answered on 02 Oct 2012, 06:15 AM
Ok, here is the code of my custom column :
A fragment of my aspx page :
And a fragment of the code-behind (juste the NeedDataSource event - I don't know if this piece of ocde) :
public class ConvertedBoolColumn : GridBoundColumn{ public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem) { if (inItem is GridDataItem) { if (cell.IsNotNull() && cell.Controls.Count > 0) { cell.Controls.Clear(); } cell.Text = ""; GridDataItem item = inItem as GridDataItem; if (item.IsNotNull()) { System.Web.UI.WebControls.CheckBox newCheck = new System.Web.UI.WebControls.CheckBox(); newCheck.Visible = true; newCheck.Enabled = false; newCheck.Checked = false; if (item.DataItem.IsNotNull()) { string value = (string)item.DataItem.GetValue(DataField); newCheck.Checked = (value == "O"); cell.Controls.Add(newCheck); } } } else { base.InitializeCell(cell, columnIndex, inItem); } }}A fragment of my aspx page :
<ceg:Grid ID="grdEleves" CssClass="LargeGridClass" runat="server" OnNeedDataSource="grdEleves_NeedDataSource" OnItemCommand="grdEleve_ItemCommand" EnableExcelExport="true" EnableCsvExport="true"> <MasterTableView DataKeyNames="NumeroEleve,Ecole,Formation,Version,Annee,AnneeScolaire,NumIns"> <Columns> <telerik:GridBoundColumn UniqueName="NumeroEleve" FilterControlAltText="[[[grille.preTexteAlt]]] [[[attr.Eleve.NumEleve]]]" DataField="NumeroEleve" HeaderText="[[[attr.Eleve.NumEleve]]]" SortExpression="NumeroEleve" HeaderStyle-Width="100px" DataType="System.Int64"> </telerik:GridBoundColumn> <ceg:ConvertedBoolColumn DataField="Definitif" HeaderText="[[[attr.InscriptionEcole.Definitif]]]" AllowFiltering="true" SortExpression="Definitif" UniqueName="Definitif" HeaderStyle-Width="65px"> </ceg:ConvertedBoolColumn> <telerik:GridBoundColumn DataField="Annee" FilterControlAltText="[[[grille.preTexteAlt]]] [[[entite.DicDegre]]]" HeaderText="[[[entite.DicDegre]]]" SortExpression="Annee" UniqueName="Annee" FilterControlWidth="100%" DataFormatString="<nobr>{0}</nobr>" HeaderStyle-Width="50px"> </telerik:GridBoundColumn> </Columns> </MasterTableView></ceg:Grid>And a fragment of the code-behind (juste the NeedDataSource event - I don't know if this piece of ocde) :
protected void grdEleves_NeedDataSource(object sender, GridNeedDataSourceEventArgs e){ InscriptionEcoleSearchCriteria criteres = this.grdEleves.GetCriteria<InscriptionEcoleSearchCriteria>(); SearchResultList<InscriptionEcoleSearch> result = GestionInscriptionsHelper.Service.SearchInscriptionEcolesPourEcoleCentreAnneeAvecCriteres( ecole, centre, annee, criteres, this.grdEleves.Pagination() ); this.grdEleves.VirtualItemCount = result.Value; this.grdEleves.DataSource = result.Results;}0
Accepted
Hello Francis,
This is expected behavior as on postback the DataItem is null. The CheckBoxe state is kept in the ViewState and after the postback it is restored from there. To have access to the DataItem when it is needed, you have to hook the cell's DataBinding event and place your logic there.
I also prepared a sample based on the provided code and attached it to this thread.
Kind regards,
Kostadin
the Telerik team
This is expected behavior as on postback the DataItem is null. The CheckBoxe state is kept in the ViewState and after the postback it is restored from there. To have access to the DataItem when it is needed, you have to hook the cell's DataBinding event and place your logic there.
public class ConvertedBoolColumn : GridBoundColumn{ public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem) { if (inItem is GridDataItem) { if (cell != null && cell.Controls.Count > 0) { cell.Controls.Clear(); } cell.Text = ""; GridDataItem item = inItem as GridDataItem; if (item != null) { System.Web.UI.WebControls.CheckBox newCheck = new System.Web.UI.WebControls.CheckBox(); newCheck.Visible = true; newCheck.Enabled = false; newCheck.Checked = false; cell.DataBinding += new EventHandler(cell_DataBinding); cell.Controls.Add(newCheck); } } else { base.InitializeCell(cell, columnIndex, inItem); } } void cell_DataBinding(object sender, EventArgs e) { TableCell cell = sender as TableCell; GridDataItem parent = cell.NamingContainer as GridDataItem; DataRowView row = parent.DataItem as DataRowView; CheckBox rowCheckBox = cell.Controls[0] as CheckBox; rowCheckBox.Checked = (bool)(row[this.DataField]); }}I also prepared a sample based on the provided code and attached it to this thread.
Kind regards,
Kostadin
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
Francis
Top achievements
Rank 1
answered on 03 Oct 2012, 06:54 AM
Thank you Kostadin !
In my case, in the cell_DataBinding method, the line
give a null (the parent.DataItem object has the type of the object I give as a DataSource (InscriptionEcoleSearch in my code behind, see my older post).
So I have to use reflexion to find the value, but it works.
Thank you !
In my case, in the cell_DataBinding method, the line
DataRowView row = parent.DataItem as DataRowView;give a null (the parent.DataItem object has the type of the object I give as a DataSource (InscriptionEcoleSearch in my code behind, see my older post).
So I have to use reflexion to find the value, but it works.
Thank you !