Use ViewState to display one record per page and also navigate

A sample code to use ViewState to display one record per page and also navigate.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 48
June 02, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
A sample code to use ViewState to display one record per page and also navigate[bold]Step 1: main.aspx[/bold]


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"></asp:textbox>

<
asp:textbox id="txtProductid" style="Z-INDEX: 103; LEFT: 204px; POSITION: absolute; TOP: 43px" runat="server"></asp:textbox

<
asp:Button id="btnPrevious" style="Z-INDEX: 102; LEFT: 137px; POSITION: absolute; TOP: 126px" runat="server" Text="Previous"></asp:Button> <asp:Button id="btnNext" style="Z-INDEX: 101; LEFT: 243px; POSITION: absolute; TOP: 126px" runat="server" Text="Next"></asp:Button>

[bold]Step 2: main.aspx.vb[/bold]


Dim myconnection As New SqlConnection("server=localhost;uid=sa;password=;database=northwind")

Dim myda As New SqlDataAdapter("Select * from Products"myconnection)

Dim ds As New DataSet()

Private 
Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MyBase.Load

'Put user code to initialize the page here

Try

      myda.Fill(ds, "Products")

      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

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

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

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

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

Private Sub txtProductid_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProductid.DataBinding

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

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials