ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
WELCOME TO MY PRESENTATION
Ms. A. Sakila., I Msc Cs
? DetailView is one of the important control, that
is mainly used in master-detail scenario.
? You select the record from master control (as
example GridView) and selected record is
displayed in DetailView control.
? In general GridView control displays more
than one record, but the DetailsView control
displays single record from database table.
? When you execute your web page, DetailsView
control renders an HTML table.The
DetailsView supports both declarative and
programmatic databinding.
? DetailsView control supports the edit, insert,
delete and paging functionality.
? Write down the connection string in web.config
file. Here Database name is Employee and table
name is tblEmps.
<configuration>
<connectionStrings>
<add name="conString" connectionString="Data
Source=(local);Initial Catalog =
Employee;Integrated Security=True" />
</connectionStrings>
</configuration>
Example
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI;
public partial class Default : System.Web.UI.Page
{
SqlConnection conn;
SqlDataAdapter adapter;
DataSet ds;
SqlCommand cmd;
string cs =
ConfigurationManager.ConnectionStrings["con
String"].ConnectionString;
protected void Page_Load(object sender,
EventArgs e)
{
if (!IsPostBack)
{
PopulateDetailView();
}
}
protected void PopulateDetailView()
{
try
{
conn = new SqlConnection(cs);
adapter = new SqlDataAdapter("select *
from tblEmps", conn);
ds = new DataSet();
adapter.Fill(ds);
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
? catch (Exception ex)
{
Label1.Text = "ERROR :: " + ex.Message;
}
}
protected void
DetailsView1_PageIndexChanging(object sender,
DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
PopulateDetailView();
}
protected void DetailsView1_ModeChanging(object
sender, DetailsViewModeEventArgs e)
{
DetailsView1.ChangeMode(e.NewMode);
PopulateDetailView();
}
}
Output:
Default mode of DetailView control is ReadOnly.
You can change this default behavior by using
ChangeMode method.
? DetailsView1.ChangeMode(DetailsViewMode.Edit
);
DetailsViewMode is an enaum type that supports
the following options.
Edit
? Insert
? ReadOnly
? In DetailView paging is not enabled by default. For
achieve paging set AllowPaging property as True.
? If user clicks on paging link at bottom of DetailView
control, then PageIndexChanging event fires.
? Another important property of DetailView control is
PagerSettings.
? This property is used to customize the look of the
paging interface.
? For example, you can display first, previous, next, and
last links instead of page numbers in DetailView.
? User can also update the existing database
records. DetailsView control has the properety
AutoGenerateEditButton.
? In order to update a record, set the value of
AutoGenerateEditButton property as true.
? The AutoGenerateEditButton property
automatically generates the Edit link at the bottom
of DetailView control.
? Next provide the value of DataKeyNames
property. The DataKeyNames property holds the
name of the primary key column of database table.
? If you do not assign value in DataKeyNames
property,
? then you will not get the primary key for
performing updating or deleting the record.
? When user click on "Edit" link, update and
cancel link will appears in-place of edit link.
?
? When user clicks on "Edit" link, ItemUpdating
event fires.
protected void DetailsView1_ItemUpdating(object
sender, DetailsViewUpdateEventArgs e )
{
int ID =
Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text);
TextBox txtName =
DetailsView1.Rows[1].Cells[1].Controls[0] as TextBox;
TextBox txtGender =
DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox;
TextBox txtSalary =
DetailsView1.Rows[3].Cells[1].Controls[0] as TextBox;
TextBox txtAddress =
DetailsView1.Rows[4].Cells[1].Controls[0] as TextBox;
TextBox txtDepartmentID =
DetailsView1.Rows[5].Cells[1].Controls[0] as TextBox;
string updateQuery = "update tblEmps set name='" +
txtName.Text + "',gender='"
+ txtGender.Text + "',Salary=" + txtSalary.Text +
",Address='" + txtAddress.Text + "',DepID="
+ txtDepartmentID.Text + " where
EmpID=" + ID;
conn = new SqlConnection(cs);
cmd = new SqlCommand(updateQuery,
conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DetailsView1.ChangeMode(DetailsViewMode
.ReadOnly);
PopulateDetailView();
}
When you perform edit operation, then by default you will not get any type of validation. You cannot restrict the user to enter nul
? You cannot restrict the user to enter null values or
illegal value.
? If you need to provide validation with input data,
then you should use templates with the
DetailsView control.
<Fields>
<asp:TemplateField HeaderText="Title:">
<EditItemTemplate>
<asp:TextBox id="txtName" Text='<%#
Bind("Name") %>' runat="server" />
<asp:RequiredFieldValidator id="reqName"
ControlToValidate=" txtName "
Text="(required)" Display="Dynamic"
Runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
? For inserting the new record into the database, first
set the value of AutoGenerateInsertButton
property as True.
? It will automatically generate the "New" link at
the bottom of DetailView control.
? When you click on the new link of DetailView
control, the UI will be changed and blank editable
text will appear.
? Now you have to find these textbox control that is
available inside the DetailView control.
? For inserting the record in database table, write the code in "ItemInserting" event as
given below.
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs
e)
{
TextBox txtEmpID = DetailsView1.Rows[0].Cells[1].Controls[0] as TextBox;
TextBox txtName = DetailsView1.Rows[1].Cells[1].Controls[0] as TextBox;
TextBox txtGender = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox;
TextBox txtSalary = DetailsView1.Rows[3].Cells[1].Controls[0] as TextBox;
TextBox txtAddress = DetailsView1.Rows[4].Cells[1].Controls[0] as TextBox;
TextBox txtDepartmentID = DetailsView1.Rows[5].Cells[1].Controls[0] as TextBox;
string insertQuery = "insert into tblEmps
values("+txtEmpID.Text+",'"+txtName.Text+"',
'"+txtGender.Text+"',"+txtSalary.Text+",'"+txtAddress.Text+"',"+txtDepartmentI
D.Text+")";
conn = new SqlConnection(cs);
cmd = new SqlCommand(insertQuery, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
PopulateDetailView();
}
? For enabling delete functionality with the DetailsView control, set
the value AutoGenerateDeleteButton property as true. It will
automatically generate the "Delete" link at the bottom of
DetailView control.
protected void DetailsView1_ItemDeleting(object sender,
DetailsViewDeleteEventArgs e)
{
int ID =
Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text);
string deleteQuery = "delete tblEmps where empid = "+ID;
conn = new SqlConnection(cs);
cmd = new SqlCommand(deleteQuery, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
PopulateDetailView();
}
Detail view in distributed technologies

More Related Content

What's hot (16)

Mysql: Tools & Gui
Mysql: Tools & GuiMysql: Tools & Gui
Mysql: Tools & Gui
DataminingTools Inc
?
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
Mani Chaubey
?
Chapter12 (1)
Chapter12 (1)Chapter12 (1)
Chapter12 (1)
Rajalaxmi Pattanaik
?
Backendless apps
Backendless appsBackendless apps
Backendless apps
Matteo Bonifazi
?
exa_cer_g23
exa_cer_g23exa_cer_g23
exa_cer_g23
Agustin Zazueta
?
Data Binding - Android by Harin Trivedi
Data Binding - Android by Harin TrivediData Binding - Android by Harin Trivedi
Data Binding - Android by Harin Trivedi
harintrivedi
?
SharePoint Conference 2018 - APIs, APIs everywhere!
SharePoint Conference 2018 - APIs, APIs everywhere!SharePoint Conference 2018 - APIs, APIs everywhere!
SharePoint Conference 2018 - APIs, APIs everywhere!
S¨¦bastien Levert
?
React outbox
React outboxReact outbox
React outbox
Angela Lehru
?
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall
tutorialsruby
?
Vb & asp .net
Vb & asp .netVb & asp .net
Vb & asp .net
Lakhmichand institute of technology
?
APIs, APIs Everywhere!
APIs, APIs Everywhere!APIs, APIs Everywhere!
APIs, APIs Everywhere!
BIWUG
?
ASP.NET - Life cycle of asp
ASP.NET - Life cycle of aspASP.NET - Life cycle of asp
ASP.NET - Life cycle of asp
priya Nithya
?
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layouts
Diego Grancini
?
SetFocus SQL Portfolio
SetFocus SQL PortfolioSetFocus SQL Portfolio
SetFocus SQL Portfolio
geometro17
?
Schema webinar
Schema webinarSchema webinar
Schema webinar
Gary Sherman
?
CaseStudy-MohammedImranAlam-Xcelsius
CaseStudy-MohammedImranAlam-XcelsiusCaseStudy-MohammedImranAlam-Xcelsius
CaseStudy-MohammedImranAlam-Xcelsius
Mohammed Imran Alam
?

Similar to Detail view in distributed technologies (20)

form view
form viewform view
form view
AbiMurugan2
?
GRID VIEW PPT
GRID VIEW PPTGRID VIEW PPT
GRID VIEW PPT
bon secours college for women,
?
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributes
sonia merchant
?
Cis407 a ilab 6 web application development devry university
Cis407 a ilab 6 web application development devry universityCis407 a ilab 6 web application development devry university
Cis407 a ilab 6 web application development devry university
lhkslkdh89009
?
Data controls ppt
Data controls pptData controls ppt
Data controls ppt
Iblesoft
?
ASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server Controls
Randy Connolly
?
Dev308
Dev308Dev308
Dev308
guest2130e
?
Android interface elements and controls-chapter8
Android interface elements and controls-chapter8Android interface elements and controls-chapter8
Android interface elements and controls-chapter8
Dr. Ramkumar Lakshminarayanan
?
Chapter 16
Chapter 16Chapter 16
Chapter 16
application developer
?
The most basic inline tag
The most basic inline tagThe most basic inline tag
The most basic inline tag
April Anne Emmanuel
?
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
Madhuri Kavade
?
Chapter 6
Chapter 6Chapter 6
Chapter 6
application developer
?
AdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CSAdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CS
Thanveen
?
Cis 407 i lab 5 of 7
Cis 407 i lab 5 of 7Cis 407 i lab 5 of 7
Cis 407 i lab 5 of 7
helpido9
?
Disconnected data
Disconnected dataDisconnected data
Disconnected data
aspnet123
?
data binding.docx
data binding.docxdata binding.docx
data binding.docx
kishorebabu123
?
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docxLab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
smile790243
?
Module05
Module05Module05
Module05
Sridhar P
?
Learning .NET Attributes
Learning .NET AttributesLearning .NET Attributes
Learning .NET Attributes
Pooja Gaikwad
?
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributes
sonia merchant
?

Recently uploaded (20)

Java Programming Fundamentals: Complete Guide for Beginners
Java Programming Fundamentals: Complete Guide for BeginnersJava Programming Fundamentals: Complete Guide for Beginners
Java Programming Fundamentals: Complete Guide for Beginners
Taranath Jaishy
?
Session-6-Developing-your-Procurement-Strategy.pptx
Session-6-Developing-your-Procurement-Strategy.pptxSession-6-Developing-your-Procurement-Strategy.pptx
Session-6-Developing-your-Procurement-Strategy.pptx
hemanthajayamanne197
?
Domino's Public Relations Crisis of 2009
Domino's Public Relations Crisis of 2009Domino's Public Relations Crisis of 2009
Domino's Public Relations Crisis of 2009
jk18647
?
2025-04-06 FATC 06 Gamaliel & Saul (shared slides).pptx
2025-04-06 FATC 06 Gamaliel & Saul (shared slides).pptx2025-04-06 FATC 06 Gamaliel & Saul (shared slides).pptx
2025-04-06 FATC 06 Gamaliel & Saul (shared slides).pptx
Dale Wells
?
AVT PASTORAL VISITS 2025 2026 AVT PASTORAL VISIT pptx
AVT PASTORAL VISITS 2025 2026 AVT PASTORAL VISIT pptxAVT PASTORAL VISITS 2025 2026 AVT PASTORAL VISIT pptx
AVT PASTORAL VISITS 2025 2026 AVT PASTORAL VISIT pptx
RamilDTolentino
?
project for hospital management system using html
project for hospital management system using htmlproject for hospital management system using html
project for hospital management system using html
xoyipib282
?
Capitalisation in history and its fundamentals
Capitalisation in history and its fundamentalsCapitalisation in history and its fundamentals
Capitalisation in history and its fundamentals
chikuniem
?
B.Sc.-Ag.-Curriculum-New-2080-AFU.ppt form
B.Sc.-Ag.-Curriculum-New-2080-AFU.ppt formB.Sc.-Ag.-Curriculum-New-2080-AFU.ppt form
B.Sc.-Ag.-Curriculum-New-2080-AFU.ppt form
Agriculture and forestry university
?
CIRAD¡¯s Experience in Research & Development Partnerships with Non-Academic A...
CIRAD¡¯s Experience in Research & Development Partnerships with Non-Academic A...CIRAD¡¯s Experience in Research & Development Partnerships with Non-Academic A...
CIRAD¡¯s Experience in Research & Development Partnerships with Non-Academic A...
Francois Stepman
?
Diabetic_patients_with_ACS who should I treat
Diabetic_patients_with_ACS who should I treatDiabetic_patients_with_ACS who should I treat
Diabetic_patients_with_ACS who should I treat
Mohammed Ahmed Bamashmoos
?
Steve Nickel Tis The Season 04.06.2025.pptx
Steve Nickel Tis The Season 04.06.2025.pptxSteve Nickel Tis The Season 04.06.2025.pptx
Steve Nickel Tis The Season 04.06.2025.pptx
FamilyWorshipCenterD
?
Leadership soft skills presentation .ppt
Leadership soft skills presentation .pptLeadership soft skills presentation .ppt
Leadership soft skills presentation .ppt
chkirankumar1912
?
Å®ÐԤηþ×°?ÄÐÐԤηþ×° as vestimentas japonesas na historia
Å®ÐԤηþ×°?ÄÐÐԤηþ×° as vestimentas japonesas na historiaÅ®ÐԤηþ×°?ÄÐÐԤηþ×° as vestimentas japonesas na historia
Å®ÐԤηþ×°?ÄÐÐԤηþ×° as vestimentas japonesas na historia
erickasrezende
?
Keeping Pets A Blessing or a Curse .pptx
Keeping Pets A Blessing or a Curse .pptxKeeping Pets A Blessing or a Curse .pptx
Keeping Pets A Blessing or a Curse .pptx
AaradhyaDixit6
?
2017-Graduation-Powerpoint-Presentation-1.ppt
2017-Graduation-Powerpoint-Presentation-1.ppt2017-Graduation-Powerpoint-Presentation-1.ppt
2017-Graduation-Powerpoint-Presentation-1.ppt
Sreeram212519
?
case presentation for pneumonia rle group 2
case presentation for pneumonia rle group 2case presentation for pneumonia rle group 2
case presentation for pneumonia rle group 2
MeegsEstabillo2
?
Joint Aviation Authorities JAA Releability Program Training
Joint Aviation Authorities JAA Releability Program TrainingJoint Aviation Authorities JAA Releability Program Training
Joint Aviation Authorities JAA Releability Program Training
timstas1
?
Growth Monitoring and Promotion (GMP) in Ethiopia.ppt
Growth Monitoring and Promotion (GMP) in Ethiopia.pptGrowth Monitoring and Promotion (GMP) in Ethiopia.ppt
Growth Monitoring and Promotion (GMP) in Ethiopia.ppt
degnet1
?
3 Circles Gospel Powerpoint 04 06 2025.pptx
3 Circles Gospel Powerpoint 04 06 2025.pptx3 Circles Gospel Powerpoint 04 06 2025.pptx
3 Circles Gospel Powerpoint 04 06 2025.pptx
FamilyWorshipCenterD
?
Research Report on International Students Presentation
Research Report on International Students PresentationResearch Report on International Students Presentation
Research Report on International Students Presentation
al20303
?
Java Programming Fundamentals: Complete Guide for Beginners
Java Programming Fundamentals: Complete Guide for BeginnersJava Programming Fundamentals: Complete Guide for Beginners
Java Programming Fundamentals: Complete Guide for Beginners
Taranath Jaishy
?
Session-6-Developing-your-Procurement-Strategy.pptx
Session-6-Developing-your-Procurement-Strategy.pptxSession-6-Developing-your-Procurement-Strategy.pptx
Session-6-Developing-your-Procurement-Strategy.pptx
hemanthajayamanne197
?
Domino's Public Relations Crisis of 2009
Domino's Public Relations Crisis of 2009Domino's Public Relations Crisis of 2009
Domino's Public Relations Crisis of 2009
jk18647
?
2025-04-06 FATC 06 Gamaliel & Saul (shared slides).pptx
2025-04-06 FATC 06 Gamaliel & Saul (shared slides).pptx2025-04-06 FATC 06 Gamaliel & Saul (shared slides).pptx
2025-04-06 FATC 06 Gamaliel & Saul (shared slides).pptx
Dale Wells
?
AVT PASTORAL VISITS 2025 2026 AVT PASTORAL VISIT pptx
AVT PASTORAL VISITS 2025 2026 AVT PASTORAL VISIT pptxAVT PASTORAL VISITS 2025 2026 AVT PASTORAL VISIT pptx
AVT PASTORAL VISITS 2025 2026 AVT PASTORAL VISIT pptx
RamilDTolentino
?
project for hospital management system using html
project for hospital management system using htmlproject for hospital management system using html
project for hospital management system using html
xoyipib282
?
Capitalisation in history and its fundamentals
Capitalisation in history and its fundamentalsCapitalisation in history and its fundamentals
Capitalisation in history and its fundamentals
chikuniem
?
CIRAD¡¯s Experience in Research & Development Partnerships with Non-Academic A...
CIRAD¡¯s Experience in Research & Development Partnerships with Non-Academic A...CIRAD¡¯s Experience in Research & Development Partnerships with Non-Academic A...
CIRAD¡¯s Experience in Research & Development Partnerships with Non-Academic A...
Francois Stepman
?
Steve Nickel Tis The Season 04.06.2025.pptx
Steve Nickel Tis The Season 04.06.2025.pptxSteve Nickel Tis The Season 04.06.2025.pptx
Steve Nickel Tis The Season 04.06.2025.pptx
FamilyWorshipCenterD
?
Leadership soft skills presentation .ppt
Leadership soft skills presentation .pptLeadership soft skills presentation .ppt
Leadership soft skills presentation .ppt
chkirankumar1912
?
Å®ÐԤηþ×°?ÄÐÐԤηþ×° as vestimentas japonesas na historia
Å®ÐԤηþ×°?ÄÐÐԤηþ×° as vestimentas japonesas na historiaÅ®ÐԤηþ×°?ÄÐÐԤηþ×° as vestimentas japonesas na historia
Å®ÐԤηþ×°?ÄÐÐԤηþ×° as vestimentas japonesas na historia
erickasrezende
?
Keeping Pets A Blessing or a Curse .pptx
Keeping Pets A Blessing or a Curse .pptxKeeping Pets A Blessing or a Curse .pptx
Keeping Pets A Blessing or a Curse .pptx
AaradhyaDixit6
?
2017-Graduation-Powerpoint-Presentation-1.ppt
2017-Graduation-Powerpoint-Presentation-1.ppt2017-Graduation-Powerpoint-Presentation-1.ppt
2017-Graduation-Powerpoint-Presentation-1.ppt
Sreeram212519
?
case presentation for pneumonia rle group 2
case presentation for pneumonia rle group 2case presentation for pneumonia rle group 2
case presentation for pneumonia rle group 2
MeegsEstabillo2
?
Joint Aviation Authorities JAA Releability Program Training
Joint Aviation Authorities JAA Releability Program TrainingJoint Aviation Authorities JAA Releability Program Training
Joint Aviation Authorities JAA Releability Program Training
timstas1
?
Growth Monitoring and Promotion (GMP) in Ethiopia.ppt
Growth Monitoring and Promotion (GMP) in Ethiopia.pptGrowth Monitoring and Promotion (GMP) in Ethiopia.ppt
Growth Monitoring and Promotion (GMP) in Ethiopia.ppt
degnet1
?
3 Circles Gospel Powerpoint 04 06 2025.pptx
3 Circles Gospel Powerpoint 04 06 2025.pptx3 Circles Gospel Powerpoint 04 06 2025.pptx
3 Circles Gospel Powerpoint 04 06 2025.pptx
FamilyWorshipCenterD
?
Research Report on International Students Presentation
Research Report on International Students PresentationResearch Report on International Students Presentation
Research Report on International Students Presentation
al20303
?

Detail view in distributed technologies

  • 1. WELCOME TO MY PRESENTATION Ms. A. Sakila., I Msc Cs
  • 2. ? DetailView is one of the important control, that is mainly used in master-detail scenario. ? You select the record from master control (as example GridView) and selected record is displayed in DetailView control. ? In general GridView control displays more than one record, but the DetailsView control displays single record from database table.
  • 3. ? When you execute your web page, DetailsView control renders an HTML table.The DetailsView supports both declarative and programmatic databinding. ? DetailsView control supports the edit, insert, delete and paging functionality. ? Write down the connection string in web.config file. Here Database name is Employee and table name is tblEmps.
  • 4. <configuration> <connectionStrings> <add name="conString" connectionString="Data Source=(local);Initial Catalog = Employee;Integrated Security=True" /> </connectionStrings> </configuration> Example using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web.UI.WebControls; using System.Web.UI; public partial class Default : System.Web.UI.Page
  • 5. { SqlConnection conn; SqlDataAdapter adapter; DataSet ds; SqlCommand cmd; string cs = ConfigurationManager.ConnectionStrings["con String"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)
  • 6. { PopulateDetailView(); } } protected void PopulateDetailView() { try { conn = new SqlConnection(cs); adapter = new SqlDataAdapter("select * from tblEmps", conn); ds = new DataSet(); adapter.Fill(ds); DetailsView1.DataSource = ds; DetailsView1.DataBind(); }
  • 7. ? catch (Exception ex) { Label1.Text = "ERROR :: " + ex.Message; } } protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e) { DetailsView1.PageIndex = e.NewPageIndex; PopulateDetailView(); } protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e) { DetailsView1.ChangeMode(e.NewMode); PopulateDetailView(); } }
  • 8. Output: Default mode of DetailView control is ReadOnly. You can change this default behavior by using ChangeMode method. ? DetailsView1.ChangeMode(DetailsViewMode.Edit ); DetailsViewMode is an enaum type that supports the following options. Edit ? Insert ? ReadOnly
  • 9. ? In DetailView paging is not enabled by default. For achieve paging set AllowPaging property as True. ? If user clicks on paging link at bottom of DetailView control, then PageIndexChanging event fires. ? Another important property of DetailView control is PagerSettings. ? This property is used to customize the look of the paging interface. ? For example, you can display first, previous, next, and last links instead of page numbers in DetailView.
  • 10. ? User can also update the existing database records. DetailsView control has the properety AutoGenerateEditButton. ? In order to update a record, set the value of AutoGenerateEditButton property as true. ? The AutoGenerateEditButton property automatically generates the Edit link at the bottom of DetailView control. ? Next provide the value of DataKeyNames property. The DataKeyNames property holds the name of the primary key column of database table.
  • 11. ? If you do not assign value in DataKeyNames property, ? then you will not get the primary key for performing updating or deleting the record. ? When user click on "Edit" link, update and cancel link will appears in-place of edit link. ? ? When user clicks on "Edit" link, ItemUpdating event fires.
  • 12. protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e ) { int ID = Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text); TextBox txtName = DetailsView1.Rows[1].Cells[1].Controls[0] as TextBox; TextBox txtGender = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox txtSalary = DetailsView1.Rows[3].Cells[1].Controls[0] as TextBox; TextBox txtAddress = DetailsView1.Rows[4].Cells[1].Controls[0] as TextBox; TextBox txtDepartmentID = DetailsView1.Rows[5].Cells[1].Controls[0] as TextBox; string updateQuery = "update tblEmps set name='" + txtName.Text + "',gender='" + txtGender.Text + "',Salary=" + txtSalary.Text + ",Address='" + txtAddress.Text + "',DepID="
  • 13. + txtDepartmentID.Text + " where EmpID=" + ID; conn = new SqlConnection(cs); cmd = new SqlCommand(updateQuery, conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); DetailsView1.ChangeMode(DetailsViewMode .ReadOnly); PopulateDetailView(); } When you perform edit operation, then by default you will not get any type of validation. You cannot restrict the user to enter nul
  • 14. ? You cannot restrict the user to enter null values or illegal value. ? If you need to provide validation with input data, then you should use templates with the DetailsView control. <Fields> <asp:TemplateField HeaderText="Title:"> <EditItemTemplate> <asp:TextBox id="txtName" Text='<%# Bind("Name") %>' runat="server" /> <asp:RequiredFieldValidator id="reqName" ControlToValidate=" txtName " Text="(required)" Display="Dynamic" Runat="server" /> </EditItemTemplate> </asp:TemplateField> </Fields>
  • 15. ? For inserting the new record into the database, first set the value of AutoGenerateInsertButton property as True. ? It will automatically generate the "New" link at the bottom of DetailView control. ? When you click on the new link of DetailView control, the UI will be changed and blank editable text will appear. ? Now you have to find these textbox control that is available inside the DetailView control.
  • 16. ? For inserting the record in database table, write the code in "ItemInserting" event as given below. protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) { TextBox txtEmpID = DetailsView1.Rows[0].Cells[1].Controls[0] as TextBox; TextBox txtName = DetailsView1.Rows[1].Cells[1].Controls[0] as TextBox; TextBox txtGender = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox; TextBox txtSalary = DetailsView1.Rows[3].Cells[1].Controls[0] as TextBox; TextBox txtAddress = DetailsView1.Rows[4].Cells[1].Controls[0] as TextBox; TextBox txtDepartmentID = DetailsView1.Rows[5].Cells[1].Controls[0] as TextBox; string insertQuery = "insert into tblEmps values("+txtEmpID.Text+",'"+txtName.Text+"', '"+txtGender.Text+"',"+txtSalary.Text+",'"+txtAddress.Text+"',"+txtDepartmentI D.Text+")"; conn = new SqlConnection(cs); cmd = new SqlCommand(insertQuery, conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); PopulateDetailView(); }
  • 17. ? For enabling delete functionality with the DetailsView control, set the value AutoGenerateDeleteButton property as true. It will automatically generate the "Delete" link at the bottom of DetailView control. protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e) { int ID = Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text); string deleteQuery = "delete tblEmps where empid = "+ID; conn = new SqlConnection(cs); cmd = new SqlCommand(deleteQuery, conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); PopulateDetailView(); }