I have a gridview web control in one asp.net 2.0 page which has a field with mutliline texts need to be displayed and updated. I am using SQLDatasource to retrieve the data from my database and then use databinding to this gridview. When the mutliline texts showed on the gridview, all line breaks were gone. For example, the original texts came from the user in the database were:
Row 1 - text text
Row 2 - text text
Row 2 - text text
While it displayed in the gridview it looked like one singel line without any line breaks:
Row 1 - text textRow 2 - text textRow 3 - text text.
And when I clicked the Edit command, these texts would show in one single line text box too.
To resolve this problem, I had to use the TemplateField and String Replace as the following.
First, convert the Boundfield to templatefield and use mutliple line textbox for editing
To make sure I still can capture line breaks from the users, I decided to use TemplateField in my gridview instead of the default BoundField. So my gridview codes looks like:
1 <asp:TemplateField HeaderText=”DETAIL” SortExpression=”DETAIL”>
2 <EditItemTemplate>
3 <asp:TextBox ID=”TextBox1″ runat=”server” TextMode=”MultiLine” Text=’<%# Bind(”DETAIL”) %>‘></asp:TextBox>
4 </EditItemTemplate>
5 <ItemTemplate>
6 <asp:Label ID=”Label1″ runat=”server” Text=’<%# Bind(”DETAIL”) %>‘></asp:Label>
7 </ItemTemplate>
8 </asp:TemplateField>
In this way, I did not have any problem to show the multi line texts with line breaks in the edit mode since the asp.net treats it as textarea instead of standard single line textbox. Now I have to show the multi line in normal mode too.
Use string replace function to replace line breaks with “<br/>”
In the RowDataBound event of my gridview, I use the following code to change all line breaks to the HTML line break tags. In my code, I first need to look up only Data Row, in this way I can filter out the Header and Footer rows of my gridview.
If e.Row.RowType = DataControlRowType.DataRow Then
CType(e.Row.FindControl(“Label1″), Label).Text = e.Row.DataItem(“DETAIL”).ToString().Replace(vbCrLf, “<br/>”)
End If
This above code worked fine in the normal mode, but it threw an error when I went into edit mode. So I have to put another condition to exclude the edit mode.
complete code to make the multi line work well
After I add a row state detection, all my code-behind block looks like as the following.
1 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
2 If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Normal Then
3 CType(e.Row.FindControl(“Label1″), Label).Text = e.Row.DataItem(“DETAIL”).ToString().Replace(vbCrLf, “<br/>”)
4 End If
5 End Sub
Hope it will be helpful for you too! Enjoy coding.
I’d prefer reading in my native language, because my knowledge of your languange is no so well. But it was interesting! Look for some my links:
[
Reply ]
Hey even rows are not getting formstted using above code but odd rows are working fine
[
Reply ]