How to display information in a custom format using one Textbox and a Literal or Label control

Donald Symmons 3,046 Reputation points
2025-02-02T11:08:03.49+00:00

Hi,

Is there a way to display database column values in a label, exactly the way it looked in a textbox in the previous page?

What I mean is, if I insert data into database, and in the textbox where the data was typed, the data will be stored in DB and in the next page, the information is displayed as it was inserted.

example:

I want to insert details as below:

Name: My Inc.

Account: 123456

Account Type: Newbie

Station: Media Team

My Textbox TextMode is set to MultiLine

 <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>

In the above details, you can see that each data is typed on a different row. The data is then stored in the database. But when it is displayed in a label,

it shows like this:

Name: My Inc.Account: 123456Account Type: NewbieStation: Media Team

How can I make it to display as below, with the use of one asp textbox and one Literal or label control?

Name: My Inc.

Account: 123456

Account Type: Newbie

Station: Media Team

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

<div style="margin: 0 auto; padding: 5px; margin-bottom: 3%; margin-top: 4%;">
                                                <asp:Literal ID="DetailLiteral" runat="server"></asp:Literal>
                                            </div>

private void DetailsDisplay()
        {
            try
            {
                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                    using (SqlCommand cmd = new SqlCommand("SELECT Details FROM TableRecord WHERE CreatedBy=@CreatedBy", con))
                    {
                        cmd.Parameters.AddWithValue("@CreatedBy", createby.Text);
                        con.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            DetailLiteral.Text = dr["details"].ToString();
                        }
                        con.Close();
                    }
                }
            }
            catch (SqlException ex)
            {
                string msg = "Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
        }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,086 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,587 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,266 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 29,681 Reputation points
    2025-02-02T13:20:43.75+00:00

    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".


  2. Albert Kallal 5,496 Reputation points
    2025-02-02T19:16:57.6666667+00:00

    Well, I don't often use the so called DetailsView control.

    However, that control is made for your question.

    So, say this markup:

                <style>
                    .myview td:first-child {font-weight: bold}
                    .myview td {padding:10px}
                    .myview {border-style:none}
                    
                </style>
                <asp:DetailsView ID="DetailsView1" runat="server" 
                    CssClass="myview"
                    width="30%"
                    AutoGenerateRows="true" >
                </asp:DetailsView>
    
    

    And code behind is this:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    LoadMyView();
            }
    
            void LoadMyView()
            {
                string strSQL =
                    @"SELECT FirstName, LastName, City, HotelName, Description
                    FROM tblHotelsA WHERE ID = 97";
                DetailsView1.DataSource = General.MyRst(strSQL);
                DetailsView1.DataBind();
            }
    
    

    And the output is now this:

    User's image

    So, you can consider using the DetailsView control.

    Another approach is to say use a repeater, but you have to "transform" the table columns into the first row in the table - this would require code, and the DetailsView does this transform for you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.