简体   繁体   中英

The variable is not incremental after click the “Next” button using C# with asp.net web application

I am trying to filter records from database and show them on textfields one by one by clicking the "Next" button. I put the code "pos++" to increase the index whenever the "Next" button is clicked, so that each record will show on the textfield .

But it only navigates the first 2 records on textfield , it stops when I click "Next" button again. For the code below, "..." means some code is omitted for easy reading purpose.

  1. I try to catch the index with lbpos.Text = pos.ToString(); , the index is up to 1, it is not incremented, I think this is the reason it shows only 2 records on textfield.

  2. I use session to retain the state of the choosing records in dataTable Session["session"] = dt; , and retrieve it with dt = (DataTable)Session["session"];

  3. I try to see if datasource has data with

     if (Session["session"] == null) lbsession.Text = "Null"; 

    but it seems it is not null because it does not show the string "null".

Code:

namespace WebApplication1
{
    public partial class mypage : System.Web.UI.Page
    {
        private DataSet ds = new DataSet();
        private SqlDataAdapter adapter;

        DataTable dt = new DataTable();

        string connString = @"Data Source=DESKTOP- 
 J12F5GP\sqlexpress;Initial Catalog=16ThingsDB;Integrated Security=True";

        int pos;

    ...
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ...
        else if (droplistsearchby.SelectedValue == "Talked")
        {
            string query = "select * from [dbo].[Students] WHERE 
Talked like '%' + @Talked + '%'";
            com.CommandText = query;
            com.Connection = con;
            com.Parameters.AddWithValue("Talked", txtsearchby.Text);

            adapter = new SqlDataAdapter(com);
            adapter.Fill(dt);

            GridView1.DataSource = dt;
            Session["session"] = dt;
            GridView1.AutoGenerateColumns = true;
            GridView1.DataBind();

            lbrequestrows.Text = "No of records in this table: " + GridView1.Rows.Count.ToString();
            showData(pos);
            con.Close();
        }
    }

    public void showData(int index)
    {
        txtid.Text = dt.Rows[index][0].ToString();
        txtfirstname.Text = dt.Rows[index][1].ToString();
        txtlastname.Text = dt.Rows[index][2].ToString();
           ...
        txttitle.Text = dt.Rows[index][4].ToString();
        txtdept.Text = dt.Rows[index][5].ToString();
        txtcompany.Text = dt.Rows[index][6].ToString();
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        dt = (DataTable)Session["session"];
        pos++;

        lbpos.Text = pos.ToString();

        if (Session["session"] == null)
            lbsession.Text = "Null";

        lblError.Text = (dt.Rows.Count).ToString();

        if (pos >= dt.Rows.Count)
            pos = 0;

        showData(pos);
    }
}

I expect to navigate the records on textfields when button "Next".

When an HTTP request comes in, an instance of the Page class is created. When the page is finished processing, an HTTP response is sent to the client and that instance of the page class disappears. When the postback occurs, the a new instance of the Page class is created. Thus storing variables in local fields does not make sense in a Page class. You will need to store your state elsewhere.

ViewState, Session, hidden form values on the page, a database. These are all options available for storing application state.

您可以通过将pos变量初始化为int pos = 0来进行尝试;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM