Solved

ASP:BUTTON Asp.net

Posted on 2010-08-23
24
350 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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