I want to know if it is possible with one Textbox and one Literal or Label control, without having to use 4 Textboxes and 4 Literal or Label controls
The design is possible if the data (record) ends with a newline character. Basically the user presses enter. The enter key adds a newline character (\r\n) within the text entered by the user. In order to display a new line in HTML we need to converting the new line characters to an HTML <br />. Also set the literal's Mode property to "PassThrough".
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebForms.Basic.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="DetailTxt"
CssClass="form-control"
runat="server"
Font-Size="10pt"
placeholder="Type your payment details here"
TextMode="MultiLine"
Style="overflow: hidden; width: 50%; resize: none;"
oninput="Resize(this)" />
<script type="text/javascript">
function Resize(textbox) {
textbox.style.height = "";
textbox.style.height = Math.min(textbox.scrollHeight, 300) + "px";
}
</script>
<hr />
<div style="margin: 0 auto; padding: 5px; margin-bottom: 3%; margin-top: 4%;">
<asp:Literal ID="DetailLiteral" runat="server" Mode="PassThrough"></asp:Literal>
</div>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
Code behind
namespace WebForms.Basic
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string Html = DetailTxt.Text.Replace("\r\n", "<br />");
DetailLiteral.Text = Html;
}
}
}
}
However, I do not recommend this approach because it opens the page for cross site scripting attacks . Also, it is difficult to get a user to press the enter key or enter a specific format like "text: text".