• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

ASP:BUTTON Asp.net

I have an ASP.NET 3.5 Web Application I want to be able to paginate using the listview control DataPager with a button not inside the listview control ( layout , item template)

I have the button in my code behind , it does show all the names in the right format, but I wouldnt paginate.... much help is highly appreciated



Protected Sub showallButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles showallButton.Click
        ' Show All Results 
        Dim sql As String
        sql = "SELECT [First Name], [Last Name], [Prof Designation],[Specialty], [Specialty2],[Address], [Address2], [City], [State], [Zip], [Phone], [Fax] FROM [tbl_Providers] ORDER BY [Last Name], [Specialty] "
        AccessDataSource2.SelectCommand = sql
        ListView1.DataBind()
        'Dim DataPager As New DataPager

    End Sub



aspx

 <asp:DataPager ID="DataPager1" runat="server" PageSize="12">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                    ShowLastPageButton="True" />
                            </Fields>
                        </asp:DataPager>

Open in new window

0
TonyReba
Asked:
TonyReba
  • 13
  • 11
1 Solution
 
AidiakapiCommented:
You'll need a workaround like this:
http://sunali.com/2008/06/09/using-datapager-with-code-behind-data-source/

Otherwise it's not possible.
0
 
TonyRebaAuthor Commented:
that looks promissing
How do I go about this in VB.NET?
0
 
AidiakapiCommented:
The DataSource (in the example a Linq data source) has an event, it's called selecting.

Every sub that executes an event gets the parameter e of the type EventArgs (or anything that looks like that, it differs for some events).

A example of what it would look like in VB.Net is on the bottom.

I'm not a real VB.Net programmer, but I used it sometimes in offline applications, I don't know if the event also has to be defined in the .aspx file, but you can test that by pressing CTRL+SHIFT+B, if you get error you'll have to add this to the .aspx file's LinqDataSource:  OnSelecting="LinqDataSource1_Selecting"

Good luck with it.
<asp:LinqDataSource ID="LinqDataSource1" runat="server">
</asp:LinqDataSource>  

 
Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    e.Result = GetDataSource() ' A function that returns the data source you want to bind.
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TonyRebaAuthor Commented:
I dontsee Result  

this is my code behind? ANy clue?
Protected Sub AccessDataSource2_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles AccessDataSource2.Selecting
        If Not IsPostBack Then
            e.Cancel = True
        End If

        e.Result = GetDataSource()

    End Sub

    Protected Sub searchButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchButton.Click
        '  Dim sql3 As String
        ' sql3 = "SELECT [First_Name] AS First_Name, [Last_Name] AS Last_Name, [Prof_Designation],[Specialty], [Specialty2],[Address], [Address2], [City], [State], [Zip], [Phone], [Fax] FROM [tbl_Providers] ORDER BY [Last_Name], [Specialty] "
        'AccessDataSource2.SelectCommand = sql3
        'ListView1.DataBind()
    End Sub


    Protected Sub showallButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles showallButton.Click
        ' Show All Results 
        Dim sql As String
        sql = "SELECT [First_Name] AS First_Name, [Last_Name] AS Last_Name, [Prof_Designation],[Specialty], [Specialty2],[Address], [Address2], [City], [State], [Zip], [Phone], [Fax] FROM [tbl_Providers] ORDER BY [Last_Name], [Specialty] "
        AccessDataSource2.SelectCommand = sql
        ListView1.DataBind()
        'Dim DataPager As New DataPager
    End Sub

    Protected Sub GetDataSource()
        ListView1.DataBind()
    End Sub

Open in new window

0
 
TonyRebaAuthor Commented:
anyone?
0
 
TonyRebaAuthor Commented:
e.result is not showing
0
 
AidiakapiCommented:
I didn't receive any mail from you comments :S.
But nevertheless.

I'd say you replace your Access data source with a Linq data source, not all datasources have the same event args. I know linq has it, but I don't know about the rest.
0
 
TonyRebaAuthor Commented:
is there any other workarounD?
0
 
AidiakapiCommented:
I doubt that there is.
But just so you know, you don't actually have to use Linq, just by implementing the workaround is enough to make it work.

You could test all other datasources to see if there's another one with e.Result.

I'm sure you'll figure it out :).
0
 
TonyRebaAuthor Commented:
I am using AccessDataSource
0
 
AidiakapiCommented:
With this way you don't have to use the access data source directly, you can use any data source and then *adapt* them.
0
 
TonyRebaAuthor Commented:
dont quite undertand it . I am typing  e.Result in ,my code behind and shows me error
0
 
AidiakapiCommented:
That's because u need to use a different data source.
0
 
TonyRebaAuthor Commented:
right,, so , which other can I use?
0
 
AidiakapiCommented:
Okey I decided to play a bit with it myself (back to VB.Net :D), I got it working after some puzzling and a workaround for crafting Anonymous Types lists.

The codebox tells you what to do:
1. First add a LinqDataSource to your .aspx file, like this:
<asp:LinqDataSource ID="LinqDataSource1" runat="server" />

2. On your ListView, change the DataSourceID to:
DataSourceID="LinqDataSource1"

3. Copy this example code to your .aspx.vb file:
    Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
        If Not IsPostBack Then
            Dim t = New With {.Name = "", .Test = 0}
            Dim l = getList(t)
            While Not recordset.EOF
                l.Add(New With {.Name = recordset.Fields("Name").Value, .Test = recordset.Fields("Test").Value})
            End While
            e.Result = l
        End If
    End Sub

    Protected Function getList(Of T)(ByVal proto As T) As List(Of T)
        Return New List(Of T)
    End Function

In the selecting event you see this several times:
New With {.Name = (string), .Test = (integer)}

That line defines what values you want to get from the database.
For example you want a first name, last name and the User_ID, you have to change those lines to:
New With {.FirstName = (string), .LastName = (string), .UserID = (int)}

Then you loop through all records that the query returns, and you use l.Add() to add it to the list of results.

The New With, is what's called an Anonymous Type this is kind of an 'inline' type declaration, in the brackets you specify what properties it has.

I hope you figure it all out :). This example should show you everything.

Good luck.

Open in new window

0
 
TonyRebaAuthor Commented:
Error      3      'recordset' is not declared. It may be inaccessible due to its protection level.      C:\Documents and Settings\gtrrra00\My Documents\Visual Studio 2010\WebSites\RgvPreffered\ProviderSearch.aspx.vb      38      41      C:\...\RgvPreffered\
0
 
AidiakapiCommented:
That's because you must replace 'recordset' with the recordset you retrieve from the database.

That means you have to establish a data connection, and then execute the SQL command.
Executing the SQL command will return a recordset (a set of records), then you loop through the records, and for every record you have to assign the values.

BTW. I forgot to mention that in:
New With {.Name = "", .Test = 0}
The .Name and .Test say how you can retrieve the actual value.
<%# Eval("Name") %> in the list view will retrieve the .Name, and <%# Eval("Test") %> will retrieve the .Test value.
0
 
TonyRebaAuthor Commented:
ok great! i ll try it right now. thanks!
0
 
AidiakapiCommented:
Good luck :)
0
 
TonyRebaAuthor Commented:
I ended up creating another accessdatasource and other listview,,,,,
0
 
AidiakapiCommented:
Well, so it does work or u decided not to make use of this system?
0
 
TonyRebaAuthor Commented:
I did not used it.  I tried another approach
0
 
TonyRebaAuthor Commented:
Well only added another AccessDatasource really
0
 
AidiakapiCommented:
Well if it works, congratulations :)
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 13
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now