Saturday, 12 May 2012

How many Data/Composite controls available in asp.net?

Yes, Asp.net Provides Data/composite controls as a Classes to represent data in the form of tabular format.

all listcontrols also databound controls but they have limited representation.( not possible like table structure)

but coming to composite controls like  GridView, FormView,DetailsView,DataList,Reapter,DataGrid, are good data representation controls

the controls represent data in the form of tabular format has a base class is called Composite Controls.


CompositeDataBoundControl which provides basic funcationalities to derived types like Gridview,DetailsView,FormView are composite databound controls.

GridView :

Gridview is structured Control which exposes data in the form of tabular format. Gridview Supports many features as a simple Properties,as well as many events, supporting design time and runtime bindings.

Show Header,ShowFooter,Autogeneratedcolumns......ect

Gridview is a composite control which supports binding in 2 ways

1 Desingtime Binding : using  DataSourceControls : SqlDataSource
2.Runtime Binding : using DataSource as data Objects : DataSet,DataTable,DataView....ect.

DataSourceID is Propertie which enables binding the data to the controls at design  time.and we can use Programatically also

Ex:
 Programmatically Binding  DataSoucecontrol to Gridview
DataSourceID
 Gridview1.DataSourceID="SqlDataSource1";
 Gridview1.DataBind();

DataSource 
DataSource is Propertie which enables binding the data objects like dataset,datatable,dataview, to the controls at runtime and we can use Programatically

Programmatically Binding Data object to Gridview

GridView1.DataSouce= DataSet/DataTable/DataView as objects.(objects can't be null).
Gridview1.DataBind();

GridView  Properties, Methods, Events,Collections,Styles,Classes,Interfaces :


Properties:

GridView1.AutoGenerateColumns=true(default)/false;
GridView1.AutoGenerateDeleteButton=true/false(default)
GridView1.AutoGenerateEditButton=true/false(default)
GridView1.AutoGenerateSelectButton=true/false(default)

GridView1.ShowHeader = true(default)/false;
GridView1.ShowFooter = true/false(default);

GridView1.PageSize=10(default)---we can customize

Gridview1.EditIndex=-1(default);

GridView1.CssClass=”style1”;

------ect are important properties.

 Collections:

GridView has only one content type Collection is Columns.

Columns is a collection of Gridview.as per our data representation we have to take an relevant column.

Columns:

BoundFiled
BoundFiled : used to represent data as a simple text. (Default structure when AutoGenerateColums=”True” ). Which is only attribute based Column.not content based.
Desingtime:
<asp:GridViewID="GridView1"runat="server">
<Columns>
<asp:BoundFieldDataField="Eid"HeaderText="Eid"/>
</Columns>
</asp:GridView>
Runtime

BoundFieldbeid = newBoundField();
beid.DataField = "Eid";
beid.HeaderText = "EmployeeID";
GridView1.Columns.Add(beid);

ButtonField
ButtonField : used to represent a Child control of type Button,Link,Image.which automatically fires GridViewRowCommand event. ButtonField is not for representation of data in the data object.it is also a attribute based column.
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="false">

<Columns>
<asp:ButtonFieldDataTextField="EName"ButtonType="Link"AccessibleHeaderText="Employees"/>
</Columns>
</asp:GridView>
Properties :

CausesValidation: boolean property to perform validations

DataTextFiled is data show as a text to our child controls of ButtonType(Button,Image,Link).

CheckBoxField

CheckBoxFiled is used to perform operations like true/false column based representation.

Like database Status Fields.

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="false">

<Columns>
<asp:CheckBoxFieldDataField="Status"HeaderText="availbality"/>
</Columns>
</asp:GridView>

HyperLinkFiled:

HyperLinkFiled is used navigations to other pages.it has 4 basic properties:

DataTextFiled : used to display data as HyperText inside gridview. // Display as link

DataNatigateUrlFileds : used to represent data Fields. //  Filed to Particular

NavigationUrl: per a static navigation.

DataNavigateUrlFormatString : used to Navigation Page. // Navigation url

<asp:HyperLinkFieldDataNavigateUrlFormatString="PersonalInfo.aspx?value={0}"DataNavigateUrlFields="Eid"DataTextField="Elocation"/>

ImageField
Is used to display image inside gridview.which as similar like HyperlinkFieldbehavioursDataImageUrlField…ect.

CommandField

CommandField display command buttonscommand buttons to perform selecting,
editing, inserting, or deleting operations in a data-bound control.

ButtonField,CommandField has a base class is ButtonFieldBase.forall button field.

class provides the methods and properties that are common to all button fields.
publicabstractclassButtonFieldBase : DataControlField
    {
protectedButtonFieldBase();

publicvirtualButtonTypeButtonType { get; set; }
publicvirtualboolCausesValidation { get; set; }
publicoverrideboolShowHeader { get; set; }
publicvirtualstringValidationGroup { get; set; }

protectedoverridevoidCopyProperties(DataControlFieldnewField);

}
forselecting,editing,deleting such all button Fields.

TempletFiled:TempletFieldis Custom Content Filed.so we can customize the data inside Field as per our requirement.

Used for Represent Custom Content insideDataBounControls like gridview,Formview,Detailsview…ect

publicclassTemplateField : DataControlField
    {
publicTemplateField();

publicvirtualITemplateAlternatingItemTemplate { get; set; }
publicvirtualboolConvertEmptyStringToNull { get; set; }
publicvirtualITemplateEditItemTemplate { get; set; }
publicvirtualITemplateFooterTemplate { get; set; }
publicvirtualITemplateHeaderTemplate { get; set; }
publicvirtualITemplateInsertItemTemplate { get; set; }

….ect
}

ITemplet is an interface which has Instance(Control control) method which will create instance of the controls  which inside the TempletField Control.

TempletFiled is a content based Column of Gridview. It has property is ItemTemplet Property(<ItemTemplet></ItemTemplet>)which can hold any control inside it. As well supports many Properties like

EditItemTemplet,AlternativeItemTemplet…..ect for more customization. So all these properties are more customized and Good User Experience.

As well few properties of Griview directly implement the EmptyDataRowTemplet,PagerTemplet..are return type is ITemplete.

Till we know Server Processing Blocks is
<%

Respose.Write(“welcome asp.net”)

%>
But I want retrieve data from dataobjectsso  we need DataBinding Expressions?

DataBinding Expressions:

 In asp.net when we want to retrieve data from dataobjects like Dataset,DataTable,DataView,…ect we have to write Binding Expressions.

Syntax :<%#Eval(“DataField”)%>

Ex:
Text='<%#Eval("Esal")%>'
For Formating

Text='<%#Eval("Esal","{0:c}") %>'

TemplateControl is base class for page,Usercontrols.

We can write binding expresions in 3 ways

Templetcontrol which base for Page, UserControls

1    1.      Eval(“fieldname”);  // one way
2    2.      Bind(“fieldname”);  // 2 way
3.      DataBinder.Eval(“fieldname”);  // In RADevelopement these expression automatically generate by DataBinder class

….so we can customize the data inside Templetfiled.

DataKey: Datakey is used Primarykey Field in Database.

DataKeyArray :DatakeyArray is used when our Primarykeymorethan one Column.

GridView Events:

class declaration

SqlConnection con = new SqlConnection("uid=sa;pwd=zolt123$;database=PropercodeDB");
    DataSet ds;
    SqlDataAdapter da;
    SqlParameter[] epars = new SqlParameter[8];
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {       
        EmployeeDetails();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        EmployeeDetails();
        
    }
    public void EmployeeDetails()
    {
        da = new SqlDataAdapter("select * from EmployeeDetails", con);
        ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();        
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        EmployeeDetails();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
        TextBox txtname =(TextBox) GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
      
        DropDownList ddldesign = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlDesig");
        TextBox txtesal = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
        TextBox txtpf = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
        TextBox txtaddress= (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
        TextBox txtdofjoin = (TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0];
        CheckBox chstatus = (CheckBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0];

        cmd = new SqlCommand("Emp_Update", con);
        cmd.CommandType = CommandType.StoredProcedure;

        epars[0] = new SqlParameter("@ename", txtname.Text);
        epars[1] = new SqlParameter("@designation", ddldesign.SelectedValue);
        epars[2] = new SqlParameter("@esal", txtesal.Text);
        epars[3] = new SqlParameter("@epf", txtpf.Text);
        epars[4] = new SqlParameter("@eadd", txtaddress.Text);
        epars[5] = new SqlParameter("@edoj", txtdofjoin.Text);
        epars[6] = new SqlParameter("@estatus", chstatus.Checked);
        epars[7] = new SqlParameter("@eid", s.ToString());        
        //foreach (SqlParameter item in epars)
        //{
        //    cmd.Parameters.Add(item);                   // Plz avoid of iteratation
        //}
        IEnumerator ier = epars.GetEnumerator();
        while (ier.MoveNext())
        {
             SqlParameter item=(SqlParameter)ier.Current;  // Most Prefered
             cmd.Parameters.Add(item);
        }
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
    }
 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        
        EmployeeDetails();
    }

No comments:

Post a Comment