Content

  • ASP.NET Literal control
  • Practical Example : ASP.NET Literal control

ASP.NET Literal control

  • ASP.NET Literal is Data control
  • With ASP.NET Literal we can even programmatically manipulate the Literal text from the code behind.
  • The Literal Control is useful when you want to add text to the output of the page dynamically (from the server).
  • ASP.NET Literal has Text property to bind text from back end / front end


Example :
Frond-End code
Download fluid-gallery.css
<html xmlns="http://www.w3.org/1999/xhtml">
            <head runat="server">
                <title>Literal Example</title>
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
                <link href="https://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet" />
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/baguettebox.js/1.8.1/baguetteBox.min.css" />
                <link rel="stylesheet" href="fluid-gallery.css" />
            </head>
            <body>
                <form id="form1" runat="server">
                 <div>
                <div class="container gallery-container">
            
               <h2> style="text-align: center">ASP Literal Example</h2>
                <div class="tz-gallery" style="padding:0">
            
                    <div class="row">
            
                        
                        <asp:Literal> runat="server" ID="lgallery"></asp:Literal>
                        
                    </div>
            
                </div>
            
            </div>
                </div>
                </form>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/baguettebox.js/1.8.1/baguetteBox.min.js"></script>
            <script>
                baguetteBox.run('.tz-gallery');
            </script>
            </body>
            </html>
Back-End code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
            
namespace Training
{
    public partial class Litralexample : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            Showphotos();
        }
            
        protected void Showphotos()
        {
            SqlCommand cmd = new SqlCommand("Select * from Photos order by Srno DESC", con);
            con.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    lgallery.Text += "<div class='col-sm-12 col-md-4'><a class='lightbox' href='" + sdr.GetValue(1).ToString() + "'><img src='" + sdr.GetValue(1).ToString() + "' /></a></div>";
                }
            }
            con.Close();
        }
    }
}