I have a textbox in a Datalist with text that I get from a Database. There are alot of <br>
in that text and I want linebreaks instead of <br>
, This is what it looks like:
((TextBox)(EditProductList.Items[0].FindControl("txtEditDescription"))).Text.Replace("<br>", "\r\开发者_如何学Pythonn");
<asp:DataList ID="EditProductList" runat="server">
<ItemTemplate>
<asp:TextBox ID="txtEditDescription" runat="server" TextMode="MultiLine" Height="350px"
Width="350px" Text='<%#Eval("Description") %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
I get the text but I get the <br>
as well.
You actually have to assign the string returned by Replace()
to the Text
property again.
var textBox = (TextBox)(EditProductList.Items[0].FindControl("txtEditDescription"));
textBox.Text = textBox.Text.Replace("<br>", "\r\n");
Try this instead (but it should be refactored):
Using your original code:
((TextBox)(EditProductList.Items[0].FindControl("txtEditDescription"))).Text =
((TextBox)(EditProductList.Items[0].FindControl("txtEditDescription"))).Text.Replace("<br>", "\r\n");
And refactor it like this:
TextBox textBox = ((TextBox)(EditProductList.Items[0].FindControl("txtEditDescription")));
textbox.Text = textBox.Text.Replace("<br>", "\r\n");
Notice that in the refactoring I'm replacing the lookup with a reference to the already found control.
The reason why we do it like this is that the .Replace function doesn't mutate the object in question, it returns a mutated version of the object, so to speak.
精彩评论