Introduction : In this article ,i will discuss about the use of “Literal Control” in asp.net. The lireral control is very important control in asp.net. This control is working same like as Label control. But the main feature of the literal control is that this is very light weight control as compare to Label. So we can say that we can use the literal control in place of the label control. Here i will also explain , how we can bind literal control with the data, after fetching data from sql server.
Implementation: create a web site , place a page named literal_conrol. Here place a literal control from the standard part of the toolbox, and renamed the id of this literal control as Literal_name.
Code of literal_control.aspx:
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="Literal_name" runat="server"></asp:Literal>
</div>
</form>
</body>
Code of literal_control.aspx.cs:
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class literal_control : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlDataAdapter adp = new SqlDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
// here i am explain that, how we can open a connection & attach with the database.
con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
con.Open();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
con.Close();
// here i calling the functionpagepost back in the named bind_literal that bind the literal control.
if (IsPostBack == false)
{
bind_literal();
}
}
private void bind_literal()
{
// here i open the connection if the connection is close
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// with the help of this sql query i am fetching the top first name from the table
SqlDataAdapter adp = new SqlDataAdapter("select Top(1)ename from tb_register order by id desc", con);
//here i specify the dataset, data will fill in this dataset first
DataSet ds = new DataSet();
adp.Fill(ds);
adp.Dispose();
// here i am bind the literal control with the name column of the database that will fill in the dataset
// in the first row and the first column.
// ds.Tables[0].Rows[0]["ename"]) means the data which will come in the first row and the first
//column according to the array
Literal_name.Text = Convert.ToString(ds.Tables[0].Rows[0]["ename"]);
}
}
Sql Server database script
/****** Object: Table [dbo].[tb_register] Script Date: 03/02/2011 10:21:17 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_register]') AND type in (N'U'))
DROP TABLE [dbo].[tb_register]
GO
/****** Object: Table [dbo].[tb_register] Script Date: 03/02/2011 10:21:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_register]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tb_register](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[ename] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[eaddress] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[econtact] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[eusername] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[epassword] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
END
GO
SET IDENTITY_INSERT [dbo].[tb_register] ON
INSERT [dbo].[tb_register] ([id], [ename], [eaddress], [econtact], [eusername], [epassword]) VALUES (1, N'Bharat', N'xfgjhdf', N'fgj', N'gfj', N'gfk')
INSERT [dbo].[tb_register] ([id], [ename], [eaddress], [econtact], [eusername], [epassword]) VALUES (2, N'bharti', N'hbffj', N'fjgh', N'gyk', N'lul')
INSERT [dbo].[tb_register] ([id], [ename], [eaddress], [econtact], [eusername], [epassword]) VALUES (3, N'raman', N'dfjm', N'jfk', N'gkhgl', N'ghk')
SET IDENTITY_INSERT [dbo].[tb_register] OFF
Conclusion: Through this article, you have learned how can bind literal control in asp.net.
Very informative post. It's really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about , for more details of this post check out this link...
ReplyDeletehttp://www.mindstick.com/Articles/72cc1085-93d0-4f80-adf4-ffdc3d6e8014/?Literal%20Control%20in%20ASP.Net
Thanks
https://www.youtube.com/watch?v=UaTG3kCFytM
ReplyDelete