Click here to Skip to main content
14,084,501 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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


What I have tried:

<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>

C# code
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();
        }
Posted
Comments
   
It sounds like a partial vs full postback issue...
You should learn AJAX or move away from ASP.NET...
Richard Deeming 2 days ago
   
Also, I don't see the point in storing the DataTable in the session, since absolutely everything you do results in reloading it from the database.
Gerry Schmitz 2 days ago
   
Is that code for "my app froze up"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190518.1 | Last Updated 15 May 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100