Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie quesiton about variables and ViewState 1

Status
Not open for further replies.

MrPink1138

Programmer
Jul 10, 2003
34
US
I'm an old school ASP guru trying to make the switch to ASP.NET and need some help with what is probably a pretty simple question.

I'm using a repeater element connected to a database to display a list. I'm trying to add some paging controls to it so it only shows a few records at a time. I want to have a CurrentPage variable to store which page the user is on and increment accordingly when the user clicks a button. The first time the page loads it would also need to be initialized to 1.
I've tried using a public variable and i've tried using the ViewState but can't seem to get it working right. If someone could point me in the right direction I would be grateful.

My code so far is below:
Code:
   Public CurrentPage As Integer 
    
   Sub PageLoad() 
      ItemsGet() 
   End Sub 

   Sub ItemsGet() 
    
      Dim strConnection As String 
      Dim oConn As SqlConnection 
      Dim ds As DataSet 
      Dim oRS As SqlDataAdapter 
      Dim oPds As PagedDataSource 
          
      ds = New DataSet() 
      strConnection = ConfigurationSettings.AppSettings("ConnectionString") 
      oConn = New SqlConnection (strConnection) 
      oRS = New SqlDataAdapter("spGetContractsAll", oConn) 
      oRS.Fill (ds, "results") 
      oPds = New PagedDataSource() 

      oPds.DataSource = ds.Tables("results").DefaultView 
    oPds.AllowPaging = true 
    oPds.PageSize = 3 
      oPds.CurrentPageIndex = CurrentPage 

      cmdPrev.Enabled = NOT oPds.IsFirstPage 
    cmdNext.Enabled = NOT oPds.IsLastPage 
    
      rptContracts.DataSource = oPds 
      rptContracts.DataBind() 
   End Sub 
    
   Sub cmdPrev_Click(s As Object, e As System.EventArgs) 
     CurrentPage -= 1 
     ItemsGet() 
   End Sub 

   Sub cmdNext_Click(s As Object, e As System.EventArgs) 
     CurrentPage += 1 
     ItemsGet() 
   End Sub
 
Fixed my own problem. If anyone's looking to do this type of thing.. my working code is below. It's probably not the best, most efficient code, but I'm new at this. :)

Code:
	Public CurrentPage As Integer
	
	Sub Page_Load()
		ItemsGet()
	End Sub

	Sub ItemsGet()
		if IsDBNull(ViewState("CurrentPage")) then
			CurrentPage = 1
		else
			CurrentPage = ViewState("CurrentPage")
		end if
	
		Dim strConnection As String
		Dim oConn As SqlConnection
		Dim ds As DataSet
		Dim oRS As SqlDataAdapter
		Dim oPds As PagedDataSource
			
		ds = New DataSet()
		strConnection = ConfigurationSettings.AppSettings("ConnectionString")
		oConn = New SqlConnection (strConnection)
		oRS = New SqlDataAdapter("spGetContractsAll", oConn)
		oRS.Fill (ds, "results")
		oPds = New PagedDataSource()

		oPds.DataSource = ds.Tables("results").DefaultView
        oPds.AllowPaging = true
        oPds.PageSize = 15
		oPds.CurrentPageIndex = CurrentPage

		cmdPrev.Enabled = NOT oPds.IsFirstPage
        cmdNext.Enabled = NOT oPds.IsLastPage
	
		rptContracts.DataSource = oPds
		rptContracts.DataBind()
	End Sub
	
	Sub cmdPrev_Click(s As Object, e As System.EventArgs)
         ViewState("CurrentPage") -= 1
         ItemsGet()
	End Sub

	Sub cmdNext_Click(s As Object, e As System.EventArgs)
         ViewState("CurrentPage") += 1
         ItemsGet()
	End Sub
 
Nice work Mr. P. Haven't tested it but looks good (stored it away for a rainey day - thanks).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top