Monday 12 August 2013

DETAILSVIEW CONTROL IN DETAILS


           DETAILSVIEW CONTROL 


DetailsView control is a data-bound control that renders a single record at a time. It can provide navigation option also. It can insert, update and delete the record also. When it is rendered on the page, generally it is implemented through <table> HTML tag.
Its properties like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. are implemented through style properites of <tahle> tag.
Following are some important properties that are very useful.
Behavior Properties of the DetailsView Control
AllowPagingtrue/false. Indicate whether the control should support navigation.
DataSourceGets or sets the data source object that contains the data to populate the control.
DataSourceIDIndicate the bound data source control to use (Generally used when we are using SqlDataSource or AccessDataSource to bind the data, See 1st Grid example).
AutoGenerateEditButtontrue/false. Indicates whether a separate column with edit link/button should be added to edit the record.
AutoGenerateDeleteButtontrue/false. Indicates whether a separate column with delete link/button should be added to delete the record.
AutoGenerateRowstrue/false. Indicate whether rows are automatically created for each field of the data source. The default is true.
DefaultModeread-only/insert/edit. Indicate the default display mode.
Style Properties of the DetailsView Control
AlternatingRowStyleDefines the style properties for every alternate row in the DetailsView.
EditRowStyleDefines the style properties for the row in EditView (When you click Edit button for a row, the row will appear in this style).
RowStyleDefines the style properties of the rows of the DetailsView.
PagerStyleDefines the style properties of Pager of the DetailsView. (If AllowPaging=true, the page number row appears in this style)
EmptyDataRowStyleDefines the style properties of the empty row, which appears if there is no records in the data source.
HeaderStyleDefines the style properties of the header of the DetailsView. (The column header appears in this style.)
FooterStyleDefines the style properties of the footer of DetailsView.
Appearance Properties of the DetailsView Control
CellPaddingIndicates the amount of space in pixel between the cells and the border of the DetailsView.
CellSpacingIndicates the amount of space in pixel between cells.
GridLinesBoth/Horizontal/Vertical/None. Indicates whether GrdiLines should appear or not, if yes Horizontal, Vertical or Both.
HorizontalAlignIndicates the horizontal alignment of the DetailsView.
EmptyDataTextIndicates the text to appear when there is no record in the data source.
BackImageUrlIndicates the location of the image that should display as a background of the DetailsView.
CaptionGets or sets the caption of the DetailsView.
CaptionAlignleft/center/right. Gets or sets the horizontal position of the DetailsView caption.
State Properties of DetailsView Control
RowsGets the collection of objects that represent the rows in the DetailsView.
FooterRowReturns a DetailsViewRow object that represents the footer of the DetailsView.
HeaderRowReturns a DetailsViewRow object that represents the header of the DetailsView.
PageCountGets the number of the pages required to display the records of the data source.
PageIndexGets or sets the 0-based page index.
DataKeyNamesGets an array that contains the names of the primary key field of the currently displayed rows in the DetailsViewRow.
DataKeysGets a collection of DataKey objects that represent the value of the primary key fields set in DataKeyNames property of the DetailsViewRow.
Events of the DetailsView Control
ItemCommandFires when any clickable element on the control is clicked.
ItemCreatedFires after DetailsView fully creates all rows of the record.
ItemDeleting, ItemDeletedBoth event fires when current record is deleted. The first one fires before and other fires after record is deleted.
ItemInserting, ItemInsertedBoth event fires when an item is inserted. The first one fires before and second after the item is created.
ItemUpdating, ItemUpdatedBoth event fires when an item is updated. The first one fires before and second fires after the record is updated.
ModeChanging, ModeChangedBoth event fires when DetailsView change its display mode. The first one fires before and second fires after display mode is changed.
PageIndexChanging, PageIndexChangedBoth event fires when the DetailsView move to another record. The first one fires before and second fires after page is changed.
DEMO : DetailsViewShow Source Code
AutoID13647
Namesivaa
Addresssklm
Phone9848022338
Cityvizag
Edit Delete New
12345678910...
      
// DetailsView control          
<asp:DetailsView ID="DetailsView1" runat="Server" CellPadding="4" ForeColor="#333333" GridLines="None"
     Width="100%" DataSourceID="SqlDataSource1" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="AutoID"
     AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" >
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
        <EditRowStyle BackColor="#999999" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Fields>
            <asp:TemplateField HeaderText="AutoID">
                <ItemTemplate>
                    <%# Eval("AutoID") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Address" HeaderText="Address" />
            <asp:BoundField DataField="Phone" HeaderText="Phone" />
            <asp:BoundField DataField="City" HeaderText="City" />
        </Fields>
    </asp:DetailsView>

// SqlDataSource control
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnStr %>'
     SelectCommand="Select * FROM SampleForTutorials ORDER BY [Name]"
     DeleteCommand="Delete FROM SampleForTutorials WHERE AutoID = @AutoID"
     UpdateCommand="UPDATE SampleForTutorials SET Name = @Name, Address = @Address, Phone = @Phone, City = @City WHERE AutoID = @AutoID"
     InsertCommand="INSERT INTO SampleForTutorials (Name, Address, Phone, City) VALUES (@Name, @Address, @Phone, @City)">
        <DeleteParameters>
            <asp:Parameter Name="AutoID" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="AutoID" Type="Int32" />
            <asp:Parameter Name="Name" Type="string" Size="50" />
            <asp:Parameter Name="Address" Type="string" Size="200" />
            <asp:Parameter Name="Phone"  Type="string" Size="50" />
            <asp:Parameter Name="City" Type="string" Size="20" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="string" Size="50" />
            <asp:Parameter Name="Address" Type="string" Size="200" />
            <asp:Parameter Name="Phone" Type="string" Size="50" />
            <asp:Parameter Name="City" Type="string" Size="20" />
            <asp:Parameter Name="AutoID" Type="Int32" />
        </InsertParameters>
     </asp:SqlDataSource> 
                    





No comments:

Post a Comment