What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 1275 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to show a record using DetailsView in ASP.NET?

How to show a record using DetailsView in ASP.NET?

Article posted by SheoNarayan on 11/5/2011 | Views: 6735 | Category: ASP.NET | Level: Beginner | Points: 250 red flag


To show complete details about a record, we can use DetailsView control. In this article we shall learn how to auto populate a single record columns on the page automatically and also how to explicitly specify the look and feel of a record column to display.

DetailsView

Many a times from the list of the records page, we need to provide a link against each record so that user can click on corresponding link and see the complete details about that record. If we are not using DetailsView control on the complete details page, we need to create several asp:Label controls and set its text property explicitly. DetailsView control fits in this scenario. DetailsView control is another data-bound control that helps us to display complete details about a single record at a time. It also gives us flexibility to manipulate, paginate the records.

Get hundreds of ASP.NET Tips and Tricks and ASP.NET Online training here.

In the previous article, we learnt about How to do pagination in the ListView using DataPager control? In this article,we shall learn how to show a record using DetailsView in ASP.NET?

ASPX PAGE

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="true" />


 <hr />DetailsView with custom fields <asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="false"> <Fields> <asp:TemplateField HeaderText="First Name"> <ItemTemplate> <%# Eval("FirstName") %> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Last Name" DataField="LastName" /> <asp:BoundField HeaderText="Age" DataField="Age" /> <asp:BoundField HeaderText="Active" DataField="Active" /> </Fields> </asp:DetailsView>


In the above code snippet, on .aspx page we have a DetailsView control with AutoGenerateColumns true and other with false. The first DetailsView displays all the default columns from the datasource automatically and 2nd one let us define our structure and style of the data to display.

In the code behind, we have used ADO.NET to retrieve the data from the database and specified the data source to the DetailsView control.

CODE BEHIND

string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

GetData();

}

}

 

private void GetData()

{

DataTable table = new DataTable();

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))

{

// write the sql statement to execute

string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail ORDER By AutoId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))

{

// fire Fill method to fetch the data and fill into DataTable

ad.Fill(table);

}

}

}

// specify the data source for the DetailsView

DetailsView1.DataSource = table;

// bind the data now

DetailsView1.DataBind();


DetailsView2.DataSource = table; DetailsView2.DataBind();

}

 

As we have kept AutoGeneratedRows property of the first DetailsView to true (by default it is true) so several rows corresponding to each columns of the data source will be created and shown as displayed in the below picture. The 2nd DetailsView takes care of the fields specified with their header and footer specified in the asp:BoundField and asp:TemplateField and displays the record.

Note: Even if our Data Source will have multiple records, DetailsView shows only first record from the data source.

 

OUTPUT


Hope this article was useful. Thanks for reading, hope you liked it.

Keep reading my forth coming articles. To read my series of articles on ASP.NET,click here.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

>> Write Response - Respond to this post and get points
Related Posts

In this article we will touch base some important concepts of handling concurrency in LINQ to SQL. We will first see how LINQ supports optimistic concurrency and then we will see what support LINQ provides to handle concurrency violation. We will then touch base on some fine tuning features provided by LINQ at field level and finally we will end this article by discussing two important error reporting options when concurrency conflict happens.

Dave(CodeProject) once told me not to use Wizards. Well let me tell what I come across in ASP.NET. I came across “PAGING” and I looked for examples on the NET, all were using SqldataSource control. I have nothing against the control; I must admit that Microsoft has done a very good job on it. It’s just that I am against using the wizards. I am a type of Programmer that is interested in writing what the wizard will do for me. Others might ask questions lie “Why reinvent a wheel”. My answer is it makes me a better programmer who understands better. Now in this Article we are going to write our small Paging Application still following the N-Tier Design Style without using any wizard.

In this article, I am going to describe what are the main differences between asp:LinkButton, asp:ImageButton, asp:Button and asp:HyperLink control and how/where to use them effectively.

To select multiple records from the GridView and retrieve selected records, we can follow this approach.

This article explains a common myth regarding the Viewstate and textbox.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 12:57:41 PM