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

Column's values are disappearing

3 Answers 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cecilie Nordbø
Top achievements
Rank 1
Cecilie Nordbø asked on 27 May 2011, 08:41 AM
Hi.
A column's values in a grid are disappearing when the user is doing something on the web-site, like push a button or insert an item in another grid but not when user is inserting a new item in the grid that keep the column that lose it's values. When I refresh the grid, the values are coming back. The column is keeping a bool value from the database that is displaying as text (and as a combobox when inserting items). The code is:

 
<telerik:GridTemplateColumn HeaderStyle-Width="195px" DataField="Shipregistry" HeaderText="Skipsregister" SortExpression="Shipregistry" UniqueName="Shipregistry">
                    <ItemTemplate><%#DataBinder.Eval(Container.DataItem, "Shipregistry")%></ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadComboBox DataTextField="Shipregistry" DataValueField="Shipregistry" ID="ShipregistryComboBox" HighlightTemplatedItems="true" EmptyMessage="Velg et element..." AllowCustomText="true"
                        runat="server" Height="50px"  Width="95px" SelectedValue='<%#Bind("Shipregistry") %>' AutoPostBack="true">
                        <Items>                       
                            <telerik:RadComboBoxItem runat="server" Text="norsk" Value="NOR"/>
                            <telerik:RadComboBoxItem runat="server" Text="internasjonalt" Value="NIC"/>
                        </Items>
                    </telerik:RadComboBox>       
                    <asp:RequiredFieldValidator runat="server" ErrorMessage="*"  ForeColor="Red" ControlToValidate="ShipregistryComboBox" ID="RequiredFieldValidator2"></asp:RequiredFieldValidator>   
                    </EditItemTemplate><HeaderStyle Width="195px"></HeaderStyle>
                </telerik:GridTemplateColumn>

protected void RadGridFerryReg_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridDataItem && e.Item.DataItem is Ferry)
           {              
         var item = e.Item as GridDataItem;
               var ferry = e.Item.DataItem as Ferry;
               item["Shipregistry"].Text = ferry.Shipregistry == "NOR" ? "norsk" : "internasjonalt";     
           }        
       }

Anyone have an idea why this happens? Perhaps the content is wrong in the ItemTemplate ....

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 27 May 2011, 09:59 PM
Hello Cecilie,

The ItemDataBound event will be triggered only when RadGrid is binding. This is why this approach works only when refreshing (rebind) the control.
Distinguishing the major differences between ItemCreated and ItemDataBound events

Best regards,
Daniel
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Cecilie Nordbø
Top achievements
Rank 1
answered on 30 May 2011, 03:36 PM
The question is about why the values in the column that already are there disappearing for example when the user push on a button on the web-site or do something else and I can't find out why they do.
0
Accepted
Daniel
Telerik team
answered on 31 May 2011, 02:46 PM
Hello Cecilie,

Because the DataBinder.Eval will create a DataBoundLiteralControl in the ItemTemplate. When you set some text in the cell you will clear the controls collection.
To avoid this, I recommend that you use a Label control as shown below:
if (e.Item is GridDataItem && e.Item.DataItem is Ferry)
{
    var item = e.Item as GridDataItem;
    var ferry = e.Item.DataItem as Ferry;
    (item["Shipregistry"].FindControl("Label1") as Label).Text = ferry.Shipregistry == "NOR" ? "norsk" : "internasjonalt";
}

<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Shipregistry") %>' />
</ItemTemplate>

Regards,
Daniel
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Cecilie Nordbø
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Cecilie Nordbø
Top achievements
Rank 1
Share this question
or