Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP:BUTTON Asp.net

Posted on 2010-08-23
24
Medium Priority
?
356 Views
Last Modified: 2013-11-08
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
Comment
Question by:TonyReba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 11
24 Comments
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33501656
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33501742
that looks promissing
How do I go about this in VB.NET?
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33501951
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 9

Author Comment

by:TonyReba
ID: 33502055
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33502719
anyone?
0
 
LVL 9

Author Comment

by:TonyReba
ID: 33502728
e.result is not showing
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33502760
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33503933
is there any other workarounD?
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33503977
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33505605
I am using AccessDataSource
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33505821
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33506019
dont quite undertand it . I am typing  e.Result in ,my code behind and shows me error
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33506264
That's because u need to use a different data source.
0
 
LVL 9

Author Comment

by:TonyReba
ID: 33506501
right,, so , which other can I use?
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33509072
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33510620
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
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33510748
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
 
LVL 9

Author Comment

by:TonyReba
ID: 33510763
ok great! i ll try it right now. thanks!
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33510835
Good luck :)
0
 
LVL 9

Accepted Solution

by:
TonyReba earned 0 total points
ID: 33527429
I ended up creating another accessdatasource and other listview,,,,,
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33531433
Well, so it does work or u decided not to make use of this system?
0
 
LVL 9

Author Comment

by:TonyReba
ID: 33531883
I did not used it.  I tried another approach
0
 
LVL 9

Author Comment

by:TonyReba
ID: 33531888
Well only added another AccessDatasource really
0
 
LVL 3

Expert Comment

by:Aidiakapi
ID: 33544408
Well if it works, congratulations :)
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question