A sample code to 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[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.Object, ByVal e As System.EventArgs) Handles 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
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By Sushila Bowalekar developerWorks - FREE Tools! | Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
| | | | Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
| | | | Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
| | | | Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available. FREE! Go There Now!
| | | | Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
| | | | Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
| | | | The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |