Solved

ASP:BUTTON Asp.net

Posted on 2010-08-23
24
347 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
  • 13
  • 11
24 Comments
 
LVL 3

Expert Comment

by:Aidiakapi
Comment Utility
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
Comment Utility
that looks promissing
How do I go about this in VB.NET?
0
 
LVL 3

Expert Comment

by:Aidiakapi
Comment Utility
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
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
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
Comment Utility
anyone?
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
e.result is not showing
0
 
LVL 3

Expert Comment

by:Aidiakapi
Comment Utility
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
Comment Utility
is there any other workarounD?
0
 
LVL 3

Expert Comment

by:Aidiakapi
Comment Utility
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
Comment Utility
I am using AccessDataSource
0
 
LVL 3

Expert Comment

by:Aidiakapi
Comment Utility
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
Comment Utility
dont quite undertand it . I am typing  e.Result in ,my code behind and shows me error
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 3

Expert Comment

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

Author Comment

by:TonyReba
Comment Utility
right,, so , which other can I use?
0
 
LVL 3

Expert Comment

by:Aidiakapi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ok great! i ll try it right now. thanks!
0
 
LVL 3

Expert Comment

by:Aidiakapi
Comment Utility
Good luck :)
0
 
LVL 9

Accepted Solution

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

Expert Comment

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

Author Comment

by:TonyReba
Comment Utility
I did not used it.  I tried another approach
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
Well only added another AccessDatasource really
0
 
LVL 3

Expert Comment

by:Aidiakapi
Comment Utility
Well if it works, congratulations :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now