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

Saving content from the GridHTMLEditorColumnEditor

2 Answers 72 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 09 Apr 2010, 12:33 AM
i have a grid as below:
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" Width="828px" OnNeedDataSource="RadGrid1_NeedDataSource" 
        OnUpdateCommand="RadGrid1_UpdateCommand" EnableAjaxSkinRendering="true" Skin="Default" 
        OnInsertCommand="RadGrid1_InsertCommand"
        <MasterTableView DataKeyNames="ProductID" GridLines="None" Width="100%" CommandItemDisplay="Top"
            <Columns> 
                <telerik:GridTemplateColumn HeaderText="Prod.Image" UniqueName="ProductImage"
                    <ItemTemplate> 
                        <asp:Image ID="Image1" ImageUrl='<%# Link.ToProductThumbNail(Eval("Thumbnail").ToString()) %>' 
                            runat="server" /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn HeaderText="Prod. Number" UniqueName="ProductNoImage"
                    <ItemTemplate> 
                        <asp:Image ID="Image2" ImageUrl='<%# Link.ToProductNo(Eval("ProductNo").ToString()) %>' 
                            runat="server" /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridBoundColumn DataField="Name" HeaderText="Prod.Name" UniqueName="ProdName"
                </telerik:GridBoundColumn> 
                <telerik:GridHTMLEditorColumn DataField="Description" HeaderText="Prod.Desc" UniqueName="ProdDescription"
                </telerik:GridHTMLEditorColumn> 
<%--                <telerik:GridBoundColumn DataField="Description" HeaderText="Prod.Desc" UniqueName="ProdDescription"
                </telerik:GridBoundColumn>--%> 
                <telerik:GridBoundColumn DataField="Price" HeaderText="Price" DataFormatString="{0:£0.00}" 
                    UniqueName="ProdPrice"
                </telerik:GridBoundColumn> 
                <telerik:GridTemplateColumn UniqueName="SelectProd"
                    <ItemTemplate> 
                        <asp:HyperLink ID="HyperLink1" runat="server" Text="Select" NavigateUrl='<%# "ProductDetail.aspx?CategoryID=" +  
                                                                                                      Request.QueryString["CategoryID"] +  
                                                                                                      "&amp;ProductIDProductID=" + Eval("ProductID") %>'> 
                        </asp:HyperLink> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
                <telerik:GridCheckBoxColumn Visible="false" DataField="PromoFront" HeaderText="Cat.Promo" 
                    UniqueName="PromoFront"
                </telerik:GridCheckBoxColumn> 
                <telerik:GridCheckBoxColumn Visible="false" DataField="PromoDept" HeaderText="Dept.Promo" 
                    UniqueName="PromoDept"
                </telerik:GridCheckBoxColumn> 
                <telerik:GridEditCommandColumn> 
                </telerik:GridEditCommandColumn> 
            </Columns> 
            <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for:{0}" CaptionDataField="Name"
                <FormTableItemStyle Wrap="False"></FormTableItemStyle> 
                <FormCaptionStyle></FormCaptionStyle
                <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" /> 
                <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" Height="110px" 
                    Width="100%" /> 
                <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> 
                <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle> 
                <EditColumn UpdateText="Update" UniqueName="EditCommandColumn1" CancelText="Cancel"
                </EditColumn> 
                <FormTableButtonRowStyle HorizontalAlign="Right"></FormTableButtonRowStyle> 
            </EditFormSettings> 
            <ExpandCollapseColumn Visible="False"
                <HeaderStyle Width="19px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <RowIndicatorColumn Visible="False"
                <HeaderStyle Width="20px" /> 
            </RowIndicatorColumn> 
        </MasterTableView> 
    </telerik:RadGrid> 
with the following code behind to save the data to the dabase:
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        //Get the GridEditableItem of the RadGrid      
        GridEditableItem editedItem = e.Item as GridEditableItem; 
         
 
        //Get the primary key value using the DataKeyValue.      
        string id = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ProductID"].ToString(); 
         
        //Access the textbox from the edit form template and store the values in string variables. 
        //string productno = (editedItem["ProductNo"].Controls[0] as TextBox).Text; 
        string name = (editedItem["ProdName"].Controls[0] as TextBox).Text; 
        string description = (editedItem["ProdDescription"].Controls[0] as GridHTMLEditorColumnEditor).Text; 
        string price = (editedItem["ProdPrice"].Controls[0] as TextBox).Text; 
        //string thumbnail = (editedItem["ProdThumbnail"].Controls[0] as TextBox).Text; 
        //string image = (editedItem["ProdImage"].Controls[0] as TextBox).Text; 
        string promoDept = (editedItem["PromoDept"].Controls[0] as CheckBox).Checked.ToString(); 
        string promoFront = (editedItem["PromoFront"].Controls[0] as CheckBox).Checked.ToString(); 
 
 
        try 
        { 
            CatalogAccess.UpdateProduct(id,name,description.ToString(),price,promoDept, promoFront); 
 
        } 
        catch (Exception ex) 
        { 
            RadGrid1.Controls.Add(new LiteralControl("Unable to update Category. Reason: " + ex.Message)); 
            e.Canceled = true
        } 
    } 
I keep getting the error on line 53
 Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 
 
Source Error: 
 
Line 51:         //string productno = (editedItem["ProductNo"].Controls[0] as TextBox).Text; 
Line 52:         string name = (editedItem["ProdName"].Controls[0] as TextBox).Text; 
Line 53:         string description = (editedItem["ProdDescription"].Controls[0] as GridHTMLEditorColumnEditor).Text; 
Line 54:         string price = (editedItem["ProdPrice"].Controls[0] as TextBox).Text; 
Line 55:         //string thumbnail = (editedItem["ProdThumbnail"].Controls[0] as TextBox).Text; 
I would really appreciate a solution.  I am sure I am almost there but not quite working.

Simon

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Apr 2010, 06:15 AM
Hello Simon,

You could access the content of GridHTMLEditorColumn by typecasting the control as RadEditor instead of GridHTMLEditorColumnEditor, since the GridHTMLEditorColumn renders RadEditor in Editmode.

C#:
 
GridEditFormItem item = (GridEditFormItem)e.Item; 
string description = (item["ProdDescription"].Controls[0] as RadEditor).Text;  

Hope this helps,
Shinu.
0
Simon
Top achievements
Rank 1
answered on 09 Apr 2010, 06:54 PM
Hi Shinu,

Many thanks for the tip.  It work like a dream.  I wanted to store the Html so I changed the text value to Html and work perfect.

Kindest regards

Simon

Here is my code
GridEditFormItem item = (GridEditFormItem)e.Item; 
            string description = (item["ProdDescription"].Controls[0] as RadEditor).Html; 

Tags
Grid
Asked by
Simon
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Simon
Top achievements
Rank 1
Share this question
or