Introduction:
In this article,i am going to explain about how to use detailsview control in asp.net 2.0 and the above versions.
Main:
The detailsview control is only available in asp.net 2.0 and above versions.Normally we are using
details view for displaying master-detail relationship.The usage of detailsview is somewhat similar
to gridview only,
See the below sample code,
<form id="EmpForm" runat="server">
<div>
Select Emp:
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" DataSourceID="SqlDataSource2"
DataTextField="EmpName" DataValueField="EmpID"
Width="140px">
</asp:DropDownList>
<br />
<br />
The details of the selected Emp are:--<br />
<br />
<asp:DetailsView ID="DetailsView1" runat="server"
Height="50px" Width="727px" BorderStyle="None"
BorderColor="Black" BorderWidth="1px
AutoGenerateRows="False" DataSourceID="SqlDataSource1"
AllowPaging="True">
<FooterStyle ForeColor="Blue"
BackColor="White"></FooterStyle>
<RowStyle ForeColor="Teal"></RowStyle>
<PagerStyle ForeColor="Blue" HorizontalAlign="Left"
BackColor="White"></PagerStyle>
<Fields>
<asp:BoundField DataField="EmpName"
HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="JoiningDate"
HeaderText="JoiningDate" SortExpression="JoiningDate"
HtmlEncode="False" DataFormatString="{0:d}"/>
<asp:BoundField DataField="Salary" HeaderText="Salary"
SortExpression="Salary" HtmlEncode="False"
DataFormatString="{0:C}"/>
<asp:BoundField DataField="DepartmentID"
HeaderText="DepartmentID" SortExpression="DepartmentID" />
<asp:BoundField DataField="EmpAddress"
HeaderText="EmpAddress"
SortExpression="EmpAddress" />
</Fields>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#336699"></HeaderStyle>
</asp:DetailsView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=.;
Initial Catalog=Test;User ID=sa;Password=sa"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [EmpName], [JoiningDate],
[Salary], [DepartmentID], [EmpAddress] FROM
[Emp] WHERE ([EmpID] = @EmpID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1"
Name="EmpID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$
ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT [EmpID], [EmpName] FROM
[Emp]">
</asp:SqlDataSource>
<br />
</form>
<form id="EmpForm" runat="server"> <div> Select Emp: <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="EmpName" DataValueField="EmpID" Width="140px"> </asp:DropDownList> <br /> <br /> The details of the selected Emp are:--<br /> <br /> <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="727px" BorderStyle="None" BorderColor="Black" BorderWidth="1px AutoGenerateRows="False" DataSourceID="SqlDataSource1" AllowPaging="True"> <FooterStyle ForeColor="Blue" BackColor="White"></FooterStyle> <RowStyle ForeColor="Teal"></RowStyle> <PagerStyle ForeColor="Blue" HorizontalAlign="Left" BackColor="White"></PagerStyle> <Fields> <asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" /> <asp:BoundField DataField="JoiningDate" HeaderText="JoiningDate" SortExpression="JoiningDate" HtmlEncode="False" DataFormatString="{0:d}"/> <asp:BoundField DataField="Salary" HeaderText="Salary" SortExpression="Salary" HtmlEncode="False" DataFormatString="{0:C}"/> <asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID" SortExpression="DepartmentID" /> <asp:BoundField DataField="EmpAddress" HeaderText="EmpAddress" SortExpression="EmpAddress" /> </Fields> <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#336699"></HeaderStyle> </asp:DetailsView> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.; Initial Catalog=Test;User ID=sa;Password=sa" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [EmpName], [JoiningDate], [Salary], [DepartmentID], [EmpAddress] FROM [Emp] WHERE ([EmpID] = @EmpID)"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="EmpID" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" SelectCommand="SELECT [EmpID], [EmpName] FROM [Emp]"> </asp:SqlDataSource> <br /> </form> |
Conclusion:
Hope this helps,
Happy Coding.