How to use controls ?
what is the use of datalist ,detailview,listview and formview?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajkamal SPosted May 21, 2012, 11:36 PM
how to use with example?
Rajkamal SPosted May 19, 2012, 5:29 AM
Satyapriya NayakPosted May 19, 2012, 1:25 AM
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
div>
<asp:FormView ID="EmployeeFormView"
DataKeyNames="EmployeeID"
Gridlines="Both" AllowPaging="true"
RunAt="server" onitemdeleting="EmployeeFormView_ItemDeleting"
onpageindexchanging="EmployeeFormView_PageIndexChanging"
onitemupdating="EmployeeFormView_ItemUpdating"
oniteminserted="EmployeeFormView_ItemInserted"
oniteminserting="EmployeeFormView_ItemInserting"
onitemupdated="EmployeeFormView_ItemUpdated"
onmodechanging="EmployeeFormView_ModeChanging" BackColor="#FFFF66"
BorderColor="#FF8080">
<HeaderStyle backcolor="Navy"
forecolor="#999966"/>
<RowStyle backcolor="White" ForeColor="#CC3300"/>
<EditRowStyle backcolor="#66FF99" ForeColor="#FF9966"/>
<ItemTemplate>
<table>
<tr><td align="right"><b>Employee ID:b>td><td><%# Eval("EmployeeID") %>td>tr>
<tr><td align="right"><b>First Name:b>td> <td><%# Eval("FirstName") %>td>tr>
<tr><td align="right"><b>Last Name:b>td> <td><%# Eval("LastName") %>td>tr>
<tr><td align="right"><b>Address:b>td> <td><%# Eval("Address")%>td>tr>
<tr><td align="right"><b>Designation:b>td> <td><%# Eval("Designation")%>td>tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="EditButton"
Text="Edit"
CommandName="Edit"
RunAt="server"/>
<asp:LinkButton ID="NewButton"
Text="New"
CommandName="New"
RunAt="server"/>
<asp:LinkButton ID="DeleteButton"
Text="Delete"
CommandName="Delete"
RunAt="server"/>
td>
tr>
table>
ItemTemplate>
<EditItemTemplate>
<table>
<tr><td align="right"><b>Employee ID:b>td>
<td><asp:TextBox ID="txtEmployeeID2"
Text='<%# Bind("EmployeeID") %>'
RunAt="Server" />td>tr>
<tr><td align="right"><b>First Name:b>td>
<td><asp:TextBox ID="txtFirstName2"
Text='<%# Bind("FirstName") %>'
RunAt="Server" />td>tr>
<tr><td align="right"><b>Last Name:b>td>
<td><asp:TextBox ID="txtLastName2"
Text='<%# Bind("LastName") %>'
RunAt="Server" />td>tr>
<tr><td align="right"><b>Address:b>td>
<td><asp:TextBox ID="txtAddress2"
Text='<%# Bind("Address") %>'
RunAt="Server" />td>tr>
<tr><td align="right"><b>Designation:b>td>
<td><asp:TextBox ID="txtDesignation2"
Text='<%# Bind("Designation") %>'
RunAt="Server" />td>tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="UpdateButton"
Text="Update"
CommandName="Update"
RunAt="server"/>
<asp:LinkButton ID="CancelUpdateButton"
Text="Cancel"
CommandName="Cancel"
RunAt="server"/>
td>
tr>
table>
EditItemTemplate>
<InsertItemTemplate>
<table>
<tr><td align="right"><b>Employee ID:b>td>
<td><asp:TextBox ID="txtEmployeeID1"
Text='<%# Bind("EmployeeID") %>'
RunAt="Server" />td>tr>
<tr><td align="right"><b>First Name:b>td>
<td><asp:TextBox ID="txtFirstName1"
Text='<%# Bind("FirstName") %>'
RunAt="Server" />td>tr>
<tr><td align="right"><b>Last Name:b>td>
<td><asp:TextBox ID="txtLastName1"
Text='<%# Bind("LastName") %>'
RunAt="Server" />td>tr>
<tr><td align="right"><b>Address:b>td>
<td><asp:TextBox ID="txtAddress1"
Text='<%# Bind("Address") %>'
RunAt="Server" />td>tr>
<tr><td align="right"><b>Designation:b>td>
<td><asp:TextBox ID="txtDesignation1"
Text='<%# Bind("Designation") %>'
RunAt="Server" />td>tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="InsertButton"
Text="Insert"
CommandName="Insert"
RunAt="server"/>
<asp:LinkButton ID="CancelInsertButton"
Text="Cancel"
CommandName="Cancel"
RunAt="server"/>
td>
tr>
table>
InsertItemTemplate>
asp:FormView>
form>
body>
html>
Default.aspx.cs code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
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 _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataAdapter sqlda = new SqlDataAdapter();
SqlCommand com = new SqlCommand();
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
SqlConnection conn = new SqlConnection(connStr);
dt = new DataTable();
com.Connection = conn;
com.CommandText = "SELECT * FROM Employees";
sqlda = new SqlDataAdapter(com);
sqlda.Fill(dt);
EmployeeFormView.DataSource = dt;
EmployeeFormView.DataBind();
}
protected void EmployeeFormView_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
EmployeeFormView.PageIndex = e.NewPageIndex;
bindgrid();
}
protected void EmployeeFormView_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
DataKey key = EmployeeFormView.DataKey;
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "DELETE FROM Employees WHERE EmployeeID='" + key.Value.ToString() + "'";
conn.Open();
com.ExecuteNonQuery();
conn.Close();
Response.Write( "Record deleted successfully");
bindgrid();
}
protected void EmployeeFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
DataKey key = EmployeeFormView.DataKey;
TextBox txtFirstName = (TextBox)EmployeeFormView.FindControl("txtFirstName2");
TextBox txtLastName = (TextBox)EmployeeFormView.FindControl("txtLastName2");
TextBox txtAddress = (TextBox)EmployeeFormView.FindControl("txtAddress2");
TextBox txtDesignation = (TextBox)EmployeeFormView.FindControl("txtDesignation2");
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "UPDATE Employees SET FirstName ='" + txtFirstName.Text + "',LastName ='" + txtLastName.Text + "',Address ='" + txtAddress.Text + "',Designation ='" + txtDesignation.Text + "' WHERE EmployeeID='" + key.Value.ToString() + "'";
conn.Open();
com.ExecuteNonQuery();
Response.Write("Record updated successfully");
bindgrid();
conn.Close();
}
protected void EmployeeFormView_ModeChanging(object sender, FormViewModeEventArgs e)
{
EmployeeFormView.ChangeMode(e.NewMode);
bindgrid();
if (e.NewMode == FormViewMode.Edit)
{
EmployeeFormView.AllowPaging = false;
}
else
{
EmployeeFormView.AllowPaging = true;
}
}
protected void EmployeeFormView_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
EmployeeFormView.ChangeMode(FormViewMode.ReadOnly);
}
protected void EmployeeFormView_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
EmployeeFormView.ChangeMode(FormViewMode.ReadOnly);
}
protected void EmployeeFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
{
TextBox txtEmployeeID = (TextBox)EmployeeFormView.FindControl("txtEmployeeID1");
TextBox txtFirstName = (TextBox)EmployeeFormView.FindControl("txtFirstName1");
TextBox txtLastName = (TextBox)EmployeeFormView.FindControl("txtLastName1");
TextBox txtAddress = (TextBox)EmployeeFormView.FindControl("txtAddress1");
TextBox txtDesignation = (TextBox)EmployeeFormView.FindControl("txtDesignation1");
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "INSERT INTO Employees Values('" + txtEmployeeID.Text + "','" + txtFirstName.Text + "', '" + txtLastName.Text + "', '" + txtAddress.Text + "', '" + txtDesignation.Text + "')";
conn.Open();
com.ExecuteNonQuery();
Response.Write("Record inserted successfully");
bindgrid();
conn.Close();
}
}
For Detailsview
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Detailsview._Default" %>
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
bind()
End If
End Sub
Sub bind()
con.Open()
str = "select * from employee"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "employee")
de1.DataSource = ds
de1.DataMember = "employee"
de1.DataBind()
con.Close()
End Sub
End Class
Thanks
Satyapriya NayakPosted May 19, 2012, 1:20 AM
For Datalist
Program
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
<table border="1">
<asp:DataList ID="dl1" runat="server">
<HeaderTemplate>
<font color="red"><b>Employee Detailsb>font>
HeaderTemplate>
<itemtemplate>
<tr>
<td><font color="Green"><b>IDb>font>td>
<td><font color="Green"><b>NAMEb>font>td>
<td><font color="Green"><b>SALARYb>font>td>
<td><font color="Green"><b>ADDRESSb>font>td>
<td><font color="Green"><b>DESIGNATIONb>font>td>
tr>
<tr>
<td> <font color="#ff8000"><b><%#Eval("empid")%>b>font>td>
<td> <font color="Fuchsia"><b><%#Eval("empname")%>b>font>td>
<td> <font color="#663300"><b><%#Eval("empsal")%>b>font>td>
<td> <font color="Purple"><b><%#Eval("empadd")%>b>font>td>
<td> <font color="#808040"><b><%#Eval("empdes")%>b>font>td>
tr>
itemtemplate>
asp:DataList>
table>
<table width="100%" border="0">
<tr>
<td> <asp:label id="lbl1" runat="server" BackColor="Yellow" BorderColor="Yellow"
Font-Bold="True" ForeColor="#FF3300">asp:label>td>
tr>
<tr>
<td> <asp:button id="btnPrevious" runat="server" text="Previous" Width="60px"
onclick="btnPrevious_Click">asp:button>
<asp:button id="btnNext" runat="server" text="Next" Width="60px"
onclick="btnNext_Click">asp:button>td>
tr>
table>
div>
form>
body>
html>
Default.aspx.vb code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
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 _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str ;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddatalist();
}
}
private void binddatalist()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
PagedDataSource Pds1 = new PagedDataSource();
Pds1.DataSource = ds.Tables[0].DefaultView;
Pds1.AllowPaging = true;
Pds1.PageSize = 3;
Pds1.CurrentPageIndex = CurrentPage;
lbl1.Text = "Showing Page: " + (CurrentPage + 1).ToString() + " of " + Pds1.PageCount.ToString();
btnPrevious.Enabled = !Pds1.IsFirstPage;
btnNext.Enabled = !Pds1.IsLastPage;
dl1.DataSource = Pds1;
dl1.DataBind();
con.Close();
}
public int CurrentPage
{
get
{
object s1 = this.ViewState["CurrentPage"];
if (s1 == null)
{
return 0;
}
else
{
return Convert.ToInt32(s1);
}
}
set { this.ViewState["CurrentPage"] = value; }
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
binddatalist();
}
protected void btnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
binddatalist();
}
}For listview
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ListView_in_Asp.net._Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table id="itemPlaceholderContainer" border="1">
<tr>
<th>EmpIDth>
<th>EmpNameth>
<th>Empaddressth>
<th>Salaryth>
tr>
<tr runat="server" ID="itemPlaceholder">
tr>
table>
LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblempid" runat="server" Text='<%# Eval("empid") %>' />
td>
<td>
<asp:Label ID="lblempname" runat="server" Text='<%# Eval("empname") %>' />
td>
<td>
<asp:Label ID="lblempaddress" runat="server" Text='<%# Eval("empaddress") %>' />
td>
<td>
<asp:Label ID="lblempsal" runat="server" Text='<%# Eval("empsal") %>' />
td>
tr>
ItemTemplate>
asp:ListView>
div>
form>
body>
html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
namespace ListView_in_Asp.net
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
ListView1.DataMember = "employee";
ListView1.DataSource = ds;
ListView1.DataBind();
con.Close();
}
}
}
Thanks
Rajkamal SPosted May 19, 2012, 1:09 AM
Rajkamal SPosted May 19, 2012, 1:02 AM
Satyapriya NayakPosted May 19, 2012, 12:56 AM
These controls are used to display data from some data source, like database or XML file.
Please refer the below link
http://www.beansoftware.com/ASP.NET-Tutorials/Repeater-DataList-ListView-GridView.aspx
Thanks