I have a gridview that has one column that is populated from a multi-line textbox. How can I replace the carriage returns with <br> so that I will see the line breaks in the grid?
If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Normal Then
Dim lbl As Label = DirectCast(e.Row.FindControl("lblCA"), Label)
Dim caText As String = lbl.Text
caText = caText.Replace(vbCrLf, "<br>")
lbl.Text = caText
End If
At some point this was working and running it in debug, it seems to work until I display the grid. I know I had this working but made changes to for some other requirements and it is probably something simple, so help!!
According to your description, as far as I know, I think you want to realize text new line. after tested, your code is work. Here is my tested code, please check:
Sample code:
<asp:GridView ID="NewGrid" runat="server" OnRowDataBound="NewGrid_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Corrective Action">
<ItemTemplate>
<asp:Label ID="lblCA" runat="server" Text='<%# Bind("Corrective_Action") %>' TextMode="MultiLine"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim table As New DataTable()
table.Columns.Add("Corrective_Action", GetType(String))
table.Rows.Add("12" & vbCr & vbLf & vbCr & vbLf & "123" & vbCr & vbLf & "1")
NewGrid.DataSource = table
NewGrid.DataBind()
End Sub
Protected Sub NewGrid_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Normal Then
Dim lbl As Label = DirectCast(e.Row.FindControl("lblCA"), Label)
Dim caText As String = lbl.Text
caText = caText.Replace(vbCr, "<br>")
lbl.Text = caText
End If
End Sub
Result:
Best Regards,
Eric Du
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
2 Points
10 Posts
replacing carriage returns in itemtemplate
Feb 23, 2017 07:46 PM|Rusty1968|LINK
I have a gridview that has one column that is populated from a multi-line textbox. How can I replace the carriage returns with <br> so that I will see the line breaks in the grid?
Here is the aspx code:
<asp:TemplateField HeaderText="Corrective Action">
<ItemTemplate>
<asp:Label ID="lblCA" runat="server" Text='<%# Bind("Corrective_Action") %>' TextMode="MultiLine"></asp:Label>
</ItemTemplate>
I have tried in the rowdatabound event like this:
If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Normal Then
Dim lbl As Label = DirectCast(e.Row.FindControl("lblCA"), Label)
Dim caText As String = lbl.Text
caText = caText.Replace(vbCrLf, "<br>")
lbl.Text = caText
End If
At some point this was working and running it in debug, it seems to work until I display the grid. I know I had this working but made changes to for some other requirements and it is probably something simple, so help!!
Contributor
6730 Points
2715 Posts
Re: replacing carriage returns in itemtemplate
Feb 24, 2017 07:41 AM|Eric Du|LINK
Hi Rusty1968,
According to your description, as far as I know, I think you want to realize text new line. after tested, your code is work. Here is my tested code, please check:
Sample code:
Result:
Best Regards,
Eric Du
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.