Multiline RadTextBox in a Radgrid Cell

3 posts, 0 answers
  1. Rick
    Rick avatar
    8 posts
    Member since:
    Aug 2014

    Posted 18 Mar 2015 Link to this post

    I need to have a multiline textbox inside a radgrid cell.  I can get the multiline mode to work while in edit mode but when I leave edit mode the text is displayed without the line breaks.  I am using batch editmode if it matters.

    Thanks

    Here is my aspx code....
                                                                    <telerik:GridTemplateColumn HeaderText="Test_Summary" HeaderStyle-Width="900px" 
                                                                        UniqueName="Test_Summary" DataField="Test_Summary"  ItemStyle-Wrap="true" ItemStyle-Height ="80px" >
                                                                        <HeaderStyle HorizontalAlign="Left" Width="900px" />
                                                                        <ItemStyle HorizontalAlign="Left" Width="900px" Height="80px"  VerticalAlign="Top"/>
                                                                    <ItemTemplate>
                                                                        <%# Eval("Test_Summary")%>
                                                                    </ItemTemplate>
                                                                    <EditItemTemplate>
                                                                        <telerik:RadTextBox runat="server" ID="txtTestSummary"  DataTextField="Test_Summary" Width="900" Height="80"
                                                                            TextMode="MultiLine" >
                                                                         </telerik:RadTextBox>
                                                                     </EditItemTemplate>
                                                                     </telerik:GridTemplateColumn>




  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 23 Mar 2015 Link to this post

    Hi Rick,

    With Batch Editing, if you need to persist the new lines, you need to handle the client-side OnBatchEditGetCellValue method and modify the value. Following is a simple example demonstrating such implementation:
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function getCellValue(sender, args) {
                if (args.get_columnUniqueName() == "Comment") {
                    var labelValue = args.get_cell().children[0].innerHTML;
                    var replacedNewLines = labelValue.trim().replace(/<br>/g, "\n");
                    replacedNewLines = replacedNewLines.trim().replace(/<BR>/g, "\n");
                    args.set_value(replacedNewLines);
                    args.set_cancel(true);
                }
            }
        </script>
    </telerik:RadCodeBlock>
     
    <telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" AllowMultiRowEdit="true" Width="400px">
        <MasterTableView AutoGenerateColumns="false" EditMode="Batch" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridTemplateColumn HeaderText="Notizen" UniqueName="Comment">
                    <ItemTemplate>
                        <asp:Literal Text='<%# replaceNewlines(Eval("Comment").ToString()) %>' runat="server" ID="Literal_Notizen"></asp:Literal>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadTextBox runat="server" ID="CommentTB" TextMode="MultiLine" Width="100%" />
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <ClientEvents OnBatchEditGetCellValue="getCellValue" />
        </ClientSettings>
    </telerik:RadGrid>

    And the dummy data:
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Comment", typeof(string));
        for (int i = 0; i < 5; i++)
        {
            table.Rows.Add(i, "Test \n   \n test");
        }
     
        (sender as RadGrid).DataSource = table;
    }
     
    public string replaceNewlines(string input)
    {
        return input.Replace("\n", "<br />");
    }

    Hope this helps.


    Best Regards,
    Konstantin Dikov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rick
    Rick avatar
    8 posts
    Member since:
    Aug 2014

    Posted 23 Mar 2015 Link to this post

    This worked.

    Thank you
Back to Top