The document discusses the DetailsView control in ASP.NET, which displays a single record from a database table. It describes how DetailsView supports editing, inserting, deleting and paging functionality through events like ItemUpdating, ItemInserting, and ItemDeleting. It also provides examples of connecting DetailsView to a database, handling its events, and performing CRUD operations on data in the database.
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.
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();
}