Wednesday, 22 February 2012

How to View one record per page in ASP.NET?





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SinglePage.aspx.cs"
 Inherits="SinglePage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
          
           <asp:TextBox ID="txtProductName" runat="server" 
 OnDataBinding="txtDataBind"></asp:TextBox>
           <asp:TextBox ID="txtProductid" runat="server"></asp:TextBox>
           <asp:Button ID="btnPrevious" runat="server" Text="Previous" 
 OnClick="PrevBtn"></asp:Button>
           <asp:Button ID="btnNext" runat="server" Text="Next" 
OnClick="NextBtn"></asp:Button>
       </div>
   </form>
</body>
</html>








using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SinglePage : System.Web.UI.Page
{
   DataTable dt;
   protected void Page_Load(object sender, EventArgs e)
   {
 dt = GetDataTable() as DataTable;

       if (!Page.IsPostBack)
       {
           ViewState["CurrentPos"] = 0;
           this.DataBind();
       }
   }
   protected void PrevBtn(object sender, System.EventArgs e)
   {
       try
       {
           int CurrentPos = (int)ViewState["CurrentPos"];
           if (CurrentPos > 0)
           {
               CurrentPos -= 1;
           }
           ViewState["CurrentPos"] = CurrentPos;
           this.DataBind();
       }
       catch (Exception ex)
       {
           Response.Write(ex.Message);
       }
   }

   protected void NextBtn(object sender, System.EventArgs e)
   {
       try
       {
           int CurrentPos = (int)ViewState["CurrentPos"];
           CurrentPos += 1;
           if (CurrentPos > dt.Rows.Count)
           {
               CurrentPos -= 1;
           }
           ViewState["CurrentPos"] = CurrentPos;
           this.DataBind();
       }
       catch (Exception ex)
       {
           Response.Write(ex.Message);
       }
   }

   protected void txtDataBind(Object sender, System.EventArgs e)
   {
       try
       {
           int CurrentPos = (int)ViewState["CurrentPos"];
           ViewState["CurrentPos"] = CurrentPos;
           txtProductid.Text = dt.Rows[CurrentPos]["productid"].ToString();
           txtProductName.Text = dt.Rows[CurrentPos]["productname"].ToString();
       }
       catch (Exception ex)
       {
           Response.Write(ex.Message);
       }
   }
   private DataTable GetDataTable()
   {
       //create table
       DataTable dt = new DataTable("Product");
       dt.Columns.Add("ProductID", Type.GetType("System.Int32"));
       dt.Columns.Add("ProductName", Type.GetType("System.String"));
    
       //create fields
       DataColumn[] pk = new DataColumn[1];
       pk[0] = dt.Columns["ProductID"];
       dt.PrimaryKey = pk;
       dt.Columns["ProductID"].AutoIncrement = true;
       dt.Columns["ProductID"].AutoIncrementSeed = 1;
       dt.Columns["ProductID"].ReadOnly = true;

       //fill rows
       DataRow dr;
       for (int x = 1; x <= 10; x++)
       {
           //make every other one different
           if (Math.IEEERemainder(x, 2) == 0)
           {
               dr = dt.NewRow();
               dr["ProductName"] = "Riss";
             
               dt.Rows.Add(dr);
           }
           else
           {
               dr = dt.NewRow();
               dr["ProductName"] = "Product";
           
               dt.Rows.Add(dr);

           }
       }

       return dt;
   }
}

No comments :