The next or previous button is not working after searching the item. Before searching, the next or previous button was working but after searching by id and when I click next, it will back to the first id number instead of continuous after searching number. Here is the code below
<div> <table><tr><td><asp:TextBox ID="txtsearch" runat="server" /></td> <td> <asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Button" /></td></tr></table> <br /> <table> <tr><td><asp:TextBox ID="txtid" runat="server"></asp:TextBox></td></tr> <tr><td><asp:TextBox ID="txtfirstname" runat="server"></asp:TextBox></td></tr> <tr><td> <asp:TextBox ID="txtlastname" runat="server"></asp:TextBox></td><td></td></tr> </table> <div> <asp:Button ID="btnprevious" runat="server" OnClick="btnprevious_Click" Text="Previous" /><asp:Button ID="btnnext" OnClick="btnnext_Click" runat="server" Text="Next" /> </div> </div>
public partial class WebForm1 : System.Web.UI.Page { DataTable sessionDataTable = new DataTable(); int pos = 0; DataTable dt = new DataTable(); int index = 0; int last = 0; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getbind(); } } private int DataIndex { get { object viewState = this.ViewState["DataIndex"]; return (viewState == null) ? 0 : (int)viewState; } set { this.ViewState["DataIndex"] = value; } } SqlDataAdapter da; private void getbind() { object sessionObject = this.Session["StatesDataTable"]; string querystr = ConfigurationManager.ConnectionStrings["NSRADConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(querystr); da = new SqlDataAdapter("SELECT ID,FirstName,LastName from RadTable", con); da.Fill(sessionDataTable); this.Session["StatesDataTable"] = sessionDataTable; sessionObject = sessionDataTable; DataTable dataTable = (DataTable)sessionObject; if (this.DataIndex < 0) // Roll to the last item this.DataIndex = (dataTable.Rows.Count - 1); else if (this.DataIndex > (dataTable.Rows.Count - 1)) this.DataIndex = 0; // Roll to the first item if (dataTable.Rows.Count <= 0) return; txtid.Text = dataTable.Rows[DataIndex]["ID"].ToString(); txtfirstname.Text = dataTable.Rows[DataIndex]["FirstName"].ToString(); txtlastname.Text = dataTable.Rows[DataIndex]["LastName"].ToString(); } protected void Button1_Click(object sender, EventArgs e) { search(); } private void search() { object sessionObject = this.Session["StatesDataTable"]; string querystr = ConfigurationManager.ConnectionStrings["NSRADConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(querystr); SqlDataAdapter da = new SqlDataAdapter("SELECT ID,FirstName,LastName from RadTable Where ID = '" + txtsearch.Text + "'", con); DataTable sessionDataTable = new DataTable(); da.Fill(sessionDataTable); this.Session["StatesDataTable"] = sessionDataTable; sessionObject = sessionDataTable; DataTable dataTable = (DataTable)sessionObject; // Make sure that the index is in range... if (this.DataIndex < 0) // Roll to the last item this.DataIndex = (dataTable.Rows.Count - 1); else if (this.DataIndex > (dataTable.Rows.Count - 1)) { this.DataIndex = 0; // Roll to the first item //this.DataIndex = Convert.ToInt32(txtid.Text); } if (dataTable.Rows.Count <= 0) return; txtid.Text = dataTable.Rows[DataIndex]["ID"].ToString(); txtfirstname.Text = dataTable.Rows[DataIndex]["FirstName"].ToString(); txtlastname.Text = dataTable.Rows[DataIndex]["LastName"].ToString(); } private void listban(int index) { txtid.Text = dt.Rows[index]["ID"].ToString(); txtfirstname.Text = dt.Rows[index]["FirstName"].ToString(); txtlastname.Text = dt.Rows[index]["LastName"].ToString(); } protected void btnnext_Click(object sender, EventArgs e) { if (txtsearch.Text != "") { this.DataIndex++; search(); } else { this.DataIndex++; getbind(); } } protected void btnprevious_Click(object sender, EventArgs e) { this.DataIndex--; getbind(); }
DataTable
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)