How to View one record per page in ASP.NET

Platform: ASP.NET| Category: ADO.NET

<asp:label id='Label2' style='Z-INDEX: 106; LEFT: 111px; POSITION: absolute; TOP: 83px' 
	runat='server'>Product ID</asp:label>
<asp:label id='Label1' style='Z-INDEX: 105; LEFT: 110px; POSITION: absolute; TOP: 43px' 
	runat='server'>Product Name</asp:label>
<asp:textbox id='txtProductName' style='Z-INDEX: 104; LEFT: 206px; POSITION: absolute; TOP: 83px'
	runat='server' OnDataBinding='txtDataBind'></asp:textbox>
<asp:textbox id='txtProductid' style='Z-INDEX: 103; LEFT: 204px; POSITION: absolute; TOP: 43px'
	runat='server'>
<asp:button id='btnPrevious' style='Z-INDEX: 102; LEFT: 137px; POSITION: absolute; TOP: 126px'
	runat='server' Text='Previous' OnClick ='PrevBtn'></asp:button>
<asp:button id='btnNext' style='Z-INDEX: 101; LEFT: 243px; POSITION: absolute; TOP: 126px' 
	runat='server' Text='Next' OnClick ='NextBtn'></asp:button>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
	’Put user code to initialize the page here 
	Try 
		’Fill the DataSet 
		If Not Page.IsPostBack Then 
			ViewState('CurrentPos') = 0 
			Me.DataBind() 
		End If 
	Catch ex As Exception 
		Response.Write(ex.Message & ex.StackTrace) 
	End Try 
End Sub 

protected Sub NextBtn(ByVal sender As System.Object, ByVal e As System.EventArgs)  
Try 
	Dim CurrentPos As Integer = CType(ViewState('CurrentPos'), Integer) 
	CurrentPos += 1 
	If CurrentPos > ds.Tables(0).Rows.Count Then 
		CurrentPos -= 1 
	End If 
	ViewState('CurrentPos') = CurrentPos 
	Me.DataBind() 
Catch ex As Exception 
	Response.Write(ex.Message) 
End Try 
End Sub 

protected Sub PrevBtn(ByVal sender As System.Object, ByVal e As System.EventArgs)  
Try 
	Dim CurrentPos As Integer = CType(ViewState('CurrentPos'), Integer) 
	If CurrentPos > 0 Then 
		CurrentPos -= 1 
	End If 
	ViewState('CurrentPos') = CurrentPos 
	Me.DataBind() 
Catch ex As Exception 
	Response.Write(ex.Message) 
End Try 
End Sub 

protected Sub txtDataBind(ByVal sender As Object, ByVal e As System.EventArgs)  
Try 
	Dim CurrentPos As Integer = CType(ViewState('CurrentPos'), Integer) 
	ViewState('CurrentPos') = (CurrentPos) 
	txtProductid.Text = ds.Tables(0).Rows(CurrentPos).Item('productid') 
	txtProductName.Text = ds.Tables(0).Rows(CurrentPos).Item('productname') 
Catch ex As Exception 
   	Response.Write(ex.Message) 
End Try 
End Sub 

C#


DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	//Fill the DataSet
	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 > ds.Tables[0].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 = ds.Tables[0].Rows[CurrentPos]['productid'].ToString(); 
		txtProductName.Text = ds.Tables[0].Rows[CurrentPos]['productname'].ToString ();
	}
catch (Exception ex) 
	{
		Response.Write(ex.Message) ;
	}
} 

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.