formatting text from database?
I had Datalist which retrieve aportion of atext from database and this text must be format .as if there is (.) .it started in new line. So how can I do that?
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<div class="scrollerDiv_open_about m10 ad_mun_font">
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("Id","~/Handlers/AboutUsImage.ashx?Id={0}") %>'
Width="178" Height="115" Style="float: left; padding: 10px 10px 0 10px" />
<span class="ad_mun_font_h"><asp:Label
ID="LblHeader" runat="server" Text='<%# Eval("Header") %>'></asp:Label></span>
<p>
<asp:Label 开发者_Go百科ID="LblText" runat="server" Text='<%# Eval("Text") %>'></asp:Label>.</p>
</div>
</ItemTemplate>
</asp:DataList>
Just use the Replace method, as follows:
// In your code, s would be filled in from the database.
string s = "This is trial text. I want a line break after a period character (.). I think this is what you mean but I am not certain."
s = s.Replace(".", ".\n\r")
Edit:
Given that you are binding to a datasource, it would be better to act directly on the datasource instead. For example:
foreach (var x in myDataSource)
{
x.TextField = x.TextField.Replace(".", ".\n\r");
}
myGridOrWhatever.DataSource = myDataSource;
myGridOrWhatever.DataBind();
I'm not with VS open, but I reckon this would work, so long as you are just using myDataSource to display stuff. If you were editing data in your grid or whatever, you might need to rethink this, but it should get you started.
精彩评论