How to use DetailsView control in asp.net C#.

DetailsView control used to display a single database record of table layout in ASP.Net.

Here we will learn how to use DetailsView control in ASP.Net using C#.

STEP 1:  Open Visual Studio and Design asp.net webform like below screen.

<table>
<tr>
<td align=”right”>
Name :</td>
<td>
<asp:TextBox ID=”txtname” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td align=”right”>
City :</td>
<td>
<asp:TextBox ID=”txtcity” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Button ID=”btnSave” runat=”server” onclick=”btnSave_Click” Text=”SAVE” />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:DetailsView ID=”DetailsView1″ runat=”server”
BackColor=”LightGoldenrodYellow” BorderColor=”Tan” BorderWidth=”1px”
CellPadding=”2″ DataKeyNames=”ID” ForeColor=”Black” GridLines=”None”
Height=”50px” onitemdeleting=”DetailsView1_ItemDeleting”
onitemupdating=”DetailsView1_ItemUpdating”
onmodechanging=”DetailsView1_ModeChanging”
onpageindexchanging=”DetailsView1_PageIndexChanging” Width=”125px”>
<FooterStyle BackColor=”Tan” />
<PagerStyle BackColor=”PaleGoldenrod” ForeColor=”DarkSlateBlue”
HorizontalAlign=”Center” />
<HeaderStyle BackColor=”Tan” Font-Bold=”True” />
<EditRowStyle BackColor=”DarkSlateBlue” ForeColor=”GhostWhite” />
<AlternatingRowStyle BackColor=”PaleGoldenrod” />
</asp:DetailsView>
</td>
</tr>
</table>

The ASP.Net DetailsView Example Design Output is:

How to use DetailsView Control in ASP.Net C#

How to use DetailsView Control in ASP.Net C#

Now, After doing this next step to bind the DetailsView control from Database.

Here, we first insert record in to database and fetch record from database and display it to DetailsView control in ASP.Net.

write below code on SAVE button for insert and bind data to DetailsView in ASP.net.

protected void btnSave_Click(object sender, EventArgs e)
{

SqlConnection SQLConn = new SqlConnection(“Data Source=COMPUTER_1\\SQLEXPRESS;Initial Catalog=BOOK;Integrated Security=True”);
SqlDataAdapter SQLAdapter = new SqlDataAdapter(“insert into UserMst values (‘”+  txtname.Text +”‘,’”+ txtcity.Text +”‘)”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
BindDetailsView();

}
private void BindDetailsView()
{

SqlConnection SQLConn = new SqlConnection(“Data Source=COMPUTER_1\\SQLEXPRESS;Initial Catalog=BOOK;Integrated Security=True”);
SqlDataAdapter SQLAdapter = new SqlDataAdapter(“select * from UserMst”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);
DetailsView1.DataSource = DT;
DetailsView1.DataBind();
}

The Out put of DetailsView control example is

How to use DetailsView Control in ASP.Net C#

How to use DetailsView Control in ASP.Net C#

If we are using DetailsView control in our website then we can display a single record at a time, so we must do paging with DetailsView control for show all the record one by one…..

How to do paging in DetailsView control in ASP.Net ?

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + 1 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">