Friday, October 3, 2014

Using the DataPager Control with ASP.NET 4.0 and c#

10:58 AM - By azeheruddin khan 0

Using the DataPager Control with ASP.NET 4.0 and C# 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
protected void DataPager_PreRender(object sender, EventArgs e)
        {
            ListView1.DataSource = GetDataTable();
            ListView1.DataBind();

        }

        protected DataTable GetDataTable()
        {
            DataSet lDataSet = new DataSet();
            using (SqlConnection lSqlConnection = new  SqlConnection(@"Data Source=.;Initial Catalog=AdventureWorks2008R2;Integrated Security=True"))
            {
                lSqlConnection.Open();
                using (SqlCommand lSqlCommand = new SqlCommand("select FirstName,MiddleName,LastName from Person.Person", lSqlConnection))
                {
                    using (SqlDataAdapter lSqlDataAdapter = new SqlDataAdapter(lSqlCommand))
                    {
                        lSqlDataAdapter.Fill(lDataSet);
                    }
                }

            }
            return lDataSet.Tables[0];

        }


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<asp:DataPager runat="server" ID="DataPager" PageSize="10" PagedControlID="ListView1"
        OnPreRender="DataPager_PreRender">
        <Fields>
            <asp:NumericPagerField ButtonCount="10" />
        </Fields>
    </asp:DataPager>
    <asp:ListView ID="ListView1" runat="server">
        <LayoutTemplate>
            <table border="0" cellpadding="1">
                <tr style="background-color: #E6E6FE">
                    <th align="left">
                        <asp:LinkButton ID="lnkId" runat="server">First Name</asp:LinkButton>
                    </th>
                    <th align="left">
                        <asp:LinkButton ID="lnkName" runat="server">Middle Name</asp:LinkButton>
                    </th>
                    <th align="left">
                        <asp:LinkButton ID="lnkType" runat="server">Last Name</asp:LinkButton>
                    </th>
                    <th>
                    </th>
                </tr>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label runat="server" ID="lblId"><%#Eval("FirstName") %></asp:Label>
                </td>
                <td>
                    <asp:Label runat="server" ID="lblName"><%#Eval("MiddleName") %></asp:Label>
                </td>
                <td>
                    <asp:Label runat="server" ID="lblType"><%#Eval("LastName") %></asp:Label>
                </td>
                <td>
                </td>
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr style="background-color: #EFEFEF">
                <td>
                    <asp:Label runat="server" ID="lblId"><%#Eval("FirstName")%></asp:Label>
                </td>
                <td>
                    <asp:Label runat="server" ID="lblName"><%#Eval("MiddleName") + " " + Eval("LastName")%></asp:Label>
                </td>
                <td>
                    <asp:Label runat="server" ID="lblType"><%#Eval("LastName")%></asp:Label>
                </td>
                <td>
                </td>
            </tr>
        </AlternatingItemTemplate>
    </asp:ListView>


About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top