This article shows how to bind data in data controls without using any database and also shows paging on controls. We will learn here how to bind controls with data without a database and how to handle paging on controls.
Initial Chamber
Step 1
Open your Visual Studio and create an empty website then provide a suitable name such as DataControlExp.
Step 2
In Solution Explorer you will get your empty website, then add some web forms.
DataControlExp (your empty website). Right-click and select Add New Item Web Form. Name it DataControlExp.aspx.
Design Chamber
Step 3
Open the DataControlExp.aspx file and write some code for the design of the application.
This is the ListView design phase.
Here I've designed the ListView Control and used some property as in the following:
- <div>
- <h3>ListView</h3>
- <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
- ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
- <LayoutTemplate>
- <table border="1">
- <tr>
- <th>Product </th>
- <th>Quantity </th>
- <th>Price </th>
- </tr>
- <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
- <tr>
- <td colspan="3">
- <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="2">
- <Fields>
- <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
- ShowNextPageButton="false" />
- <asp:NumericPagerField ButtonType="Link" />
- <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
- </Fields>
- </asp:DataPager>
- </td>
- </tr>
- </table>
- </LayoutTemplate>
- <GroupTemplate>
- <tr>
- <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
- </tr>
- </GroupTemplate>
- <ItemTemplate>
- <td><%# Eval("Product_Name") %>
- </td>
- <td><%# Eval("Quantity") %>
- </td>
- <td><%# Eval("Price") %>
- </td>
- </ItemTemplate>
- </asp:ListView>
- </div>
FormView control
I've designed a FormView control as in the following:
- <h3>FormView</h3>
- <div>
- <asp:FormView ID="FormView1" runat="server"
- AllowPaging="True" EnableViewState="False" OnPageIndexChanging="FormView1_PageIndexChanging" BackColor="Green">
- <ItemTemplate>
- <hr />
- <h3><%# Eval("Product_Name") %>
- </h3>
- <table border="0">
- <tr>
- <td class="ProductPropertyLabel">Quantity:</td>
- <td class="ProductPropertyValue"><%# Eval("Quantity") %>
- </td>
- <td class="ProductPropertyLabel">Price:</td>
- <td class="ProductPropertyValue"><%# Eval("Price")%>
- </td>
- </tr>
- </table>
- <hr />
- </ItemTemplate>
- </asp:FormView>
- </div>
- Here design DetailsView-
-
- <h3>DetailsView</h3>
- <div>
- <div>
- <asp:DetailsView ID="DetailsView1" OnPageIndexChanging="DetailsView1_PageIndexChanging" BackColor="Wheat"
- AllowPaging="true" runat="server" />
- </div>
- </div>
- Here design GridView-
-
- <h3>GridView</h3>
- <div>
- <asp:GridView ID="gvExample" runat="server" AllowPaging="true" PageSize="2" AutoGenerateColumns="False" BackColor="PINK" BorderColor="#E7E7FF"
- BorderWidth="1px" CellPadding="3" OnPageIndexChanging="gvExample_PageIndexChanging">
- <Columns>
- <asp:TemplateField HeaderText="Product_Name">
- <ItemTemplate>
- <asp:Label ID="lblProduct" runat="server" Text='<%#Eval("Product_Name")%>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Quantity">
- <ItemTemplate>
- <asp:Label ID="lblQuantity" runat="server" Text='<%#Eval("Quantity")%>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Price">
- <ItemTemplate>
- <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- </div>
The page looks as in the following.
DataControlExp.aspx
CODE CHAMBER
Step 4
In the code chamber we will write some code so that our application works.
DataControlExp.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
-
- public partial class DataControlExp: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack) {
- BindListView();
- BindFormView();
- BindDetailsView();
- BindGridView();
- }
-
- }
-
- protected void BindListView()
- {
-
- DataSet ds = new DataSet();
- DataTable dt;
- DataRow dr;
- DataColumn pName;
- DataColumn pQty;
- DataColumn pPrice;
-
- dt = new DataTable();
-
- pName = new DataColumn("Product_Name", Type.GetType("System.String"));
- pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
- pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
-
- dt.Columns.Add(pName);
- dt.Columns.Add(pQty);
- dt.Columns.Add(pPrice);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 1";
- dr["Quantity"] = 2;
- dr["Price"] = 200;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 2";
- dr["Quantity"] = 5;
- dr["Price"] = 480;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 3";
- dr["Quantity"] = 8;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 4";
- dr["Quantity"] = 2;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
-
- ds.Tables.Add(dt);
-
- lvCustomers.DataSource = ds.Tables[0];
- lvCustomers.DataBind();
- }
-
- protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
- {
- (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
- this.BindListView();
- }
-
- protected void BindFormView()
- {
-
-
- DataSet ds = new DataSet();
- DataTable dt;
- DataRow dr;
- DataColumn pName;
- DataColumn pQty;
- DataColumn pPrice;
-
- dt = new DataTable();
-
- pName = new DataColumn("Product_Name", Type.GetType("System.String"));
- pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
- pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
-
- dt.Columns.Add(pName);
- dt.Columns.Add(pQty);
- dt.Columns.Add(pPrice);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 1";
- dr["Quantity"] = 2;
- dr["Price"] = 200;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 2";
- dr["Quantity"] = 5;
- dr["Price"] = 480;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 3";
- dr["Quantity"] = 8;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 4";
- dr["Quantity"] = 2;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
-
- ds.Tables.Add(dt);
-
- FormView1.DataSource = ds.Tables[0];
- FormView1.DataBind();
- }
-
- protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
- {
- FormView1.PageIndex = e.NewPageIndex;
- BindFormView();
- }
-
- protected void BindDetailsView()
- {
-
- DataSet ds = new DataSet();
- DataTable dt;
- DataRow dr;
- DataColumn pName;
- DataColumn pQty;
- DataColumn pPrice;
-
- dt = new DataTable();
-
- pName = new DataColumn("Product_Name", Type.GetType("System.String"));
- pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
- pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
-
- dt.Columns.Add(pName);
- dt.Columns.Add(pQty);
- dt.Columns.Add(pPrice);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 1";
- dr["Quantity"] = 2;
- dr["Price"] = 200;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 2";
- dr["Quantity"] = 5;
- dr["Price"] = 480;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 3";
- dr["Quantity"] = 8;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 4";
- dr["Quantity"] = 2;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
-
- ds.Tables.Add(dt);
-
- DetailsView1.DataSource = ds.Tables[0];
- DetailsView1.DataBind();
- }
-
- protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
- {
- DetailsView1.PageIndex = e.NewPageIndex;
- BindDetailsView();
- }
-
- protected void BindGridView()
- {
-
- DataSet ds = new DataSet();
- DataTable dt;
- DataRow dr;
- DataColumn pName;
- DataColumn pQty;
- DataColumn pPrice;
-
- dt = new DataTable();
-
- pName = new DataColumn("Product_Name", Type.GetType("System.String"));
- pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
- pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
-
- dt.Columns.Add(pName);
- dt.Columns.Add(pQty);
- dt.Columns.Add(pPrice);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 1";
- dr["Quantity"] = 2;
- dr["Price"] = 200;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 2";
- dr["Quantity"] = 5;
- dr["Price"] = 480;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 3";
- dr["Quantity"] = 8;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
-
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 4";
- dr["Quantity"] = 2;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
-
- ds.Tables.Add(dt);
-
- gvExample.DataSource = ds.Tables[0];
- gvExample.DataBind();
-
- }
-
- protected void gvExample_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- gvExample.PageIndex = e.NewPageIndex;
- BindGridView();
- }
- }
Figure 1:Output
I hope you liked this. Have a good day. Thank you for reading.