locked
How to remove html br tag from text box? RRS feed

  • Question

  • User2019896008 posted

    Hello,

    I have a multi-line text box in my saving form and I allow users enter for line breaks.So I save it as it is to the db like below code.

    cmd.Parameters.AddWithValue("@IntroText", txtIntro.Text.Replace("\r\n", "<br />"));

    So this part works perfectly.But the issue is when I bind these values to a FormView in order to edit the records.

    <asp:TextBox ID="Leder1TextBox" runat="server" Text='<%# Bind("Leder1") %>' CssClass="txtapplicantfname" TextMode="MultiLine"/>

    It shows the <br/> tags in textbox in edit view.How to I format my multiline text box in FormView and remove <br/> tags?

    Thanks

    Sunday, February 17, 2019 5:24 PM

All replies

  • User475983607 posted

    Don't replace the /r/n characters when saving to the database.  Do the replace when writing HTML.

    Or do another replace to replace the <br /> with /r/n when writing to an input field.

    Sunday, February 17, 2019 5:33 PM
  • User61956409 posted

    Hi studio7t7,

    this part works perfectly.But the issue is when I bind these values to a FormView in order to edit the records.

    It shows the <br/> tags in textbox in edit view.How to I format my multiline text box in FormView and remove <br/> tags?

    You can replace "<br />" with "\r\n" while the FormView is in Edit mode, the following code snippet work well on my side, please refer to it.

    protected void FormView1_DataBound(object sender, EventArgs e)
    {
        if (FormView1.CurrentMode == FormViewMode.Edit)
        {
            TextBox txt_Leder1 = FormView1.FindControl("Leder1TextBox") as TextBox;
            txt_Leder1.Text = txt_Leder1.Text.Replace("<br />", "\r\n");
        }
    }

    With Regards,

    Fei Han

    Thursday, February 28, 2019 8:11 AM