| Author: Aamir 28 Sep 2008 | Member Level: Bronze | Rating:  Points: 6 |
Try this code for code behind string connectionString = @"Data Source=l-msaam-jlg6t1s\sqlexpress;Integrated Security=True;Initial Catalog=Northwind"; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { intPageSize.Text = "4"; intCurrIndex.Text = "0"; DataBind(); } }
private void DataBind() { SqlDataAdapter da = new SqlDataAdapter("Select orderid,customerid from orders",connectionString); DataSet ds = new DataSet();
if (!Page.IsPostBack) { da.Fill(ds); intRecordCount.Text = ds.Tables[0].Rows.Count.ToString(); ; ds = null; ds = new DataSet(); } da.Fill(ds, int.Parse(intCurrIndex.Text), int.Parse(intPageSize.Text), "Orders"); dList.DataSource = ds.Tables[0].DefaultView; dList.DataBind();
} protected void prevbutton_Click(object sender, EventArgs e) { intCurrIndex.Text = Convert.ToString((int.Parse(intCurrIndex.Text) - int.Parse(intPageSize.Text)));
if (int.Parse(intCurrIndex.Text) < 0) { intCurrIndex.Text = "0"; } DataBind();
} protected void nextButton_Click(object sender, EventArgs e) { if (int.Parse(intCurrIndex.Text) + 1 < int.Parse(intRecordCount.Text)) { intCurrIndex.Text = Convert.ToString((int.Parse(intCurrIndex.Text) + int.Parse(intPageSize.Text))); } if (int.Parse(intCurrIndex.Text) >= int.Parse(intRecordCount.Text)) { intCurrIndex.Text = Convert.ToString(int.Parse(intCurrIndex.Text) - int.Parse(intPageSize.Text)); }
DataBind(); }
// For aspx page HTML
<div> <asp:DataList ID="dList" Runat="server" Width="560px" ItemStyle-BackColor="Beige" EnableViewState="False"> <HeaderTemplate> <table width="100%" style="font: 10pt verdana" cellpadding="0" cellspacing="0"> <tr style="background-color:FF0000"> <th align="left"> <font color="#FFFFFF">Customer ID</font></th> <th align="left"> <font color="#FFFFFF">Order ID</font></th> <th align="left"> <font color="#FFFFFF">Customer ID</font></th> </tr> </HeaderTemplate> <ItemTemplate> <tr style="background-color:#f5f5dc"> <td><%# DataBinder.Eval(Container.DataItem, "customerid")%></td> <td><%# DataBinder.Eval(Container.DataItem, "orderid") %></td> <td><%# DataBinder.Eval(Container.DataItem, "customerid")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:DataList> <asp:Button id="prevbutton" runat="server" Text="Prev" OnClick="prevbutton_Click"></asp:Button> <asp:Button id="nextButton" runat="server" Text="Next" OnClick="nextButton_Click"></asp:Button> <asp:label ID="intCurrIndex" Visible="False" Runat="server" /> <asp:label ID="intPageSize" Visible="False" Runat="server" /> <asp:label ID="intRecordCount" Visible="False" Runat="server" /> </div>
|