C# 我的柜台坏了

C# 我的柜台坏了,c#,asp.net,counter,C#,Asp.net,Counter,大家好,有人能帮忙吗?我不知道为什么我的“下一步”按钮中的计数器不起作用。很抱歉,所有其他代码。我想通过使用计数器在数据库中生成贯穿数据。如果有一种更简单的方法,它也会起作用 public partial class _Default : System.Web.UI.Page { int iDefualt = 2; int iMainCounter = 0; SqlConnection con1 = new SqlConnection("Data Source=EON;I

大家好,有人能帮忙吗?我不知道为什么我的“下一步”按钮中的计数器不起作用。很抱歉,所有其他代码。我想通过使用计数器在数据库中生成贯穿数据。如果有一种更简单的方法,它也会起作用

public partial class _Default : System.Web.UI.Page
{
    int iDefualt = 2;
    int iMainCounter = 0;
    SqlConnection con1 = new SqlConnection("Data Source=EON;Initial Catalog=DW2;Persist Security Info=True;User ID=Kapow;Password=Kapow");
    DataTable dt = new DataTable();              

    public void Page_Load(object sender, EventArgs e)
    {
        con1.Open();
        SqlDataReader myReader = null;
        //SqlCommand myCommand = new SqlCommand("select * from customer_registration where username='" + Session["username"] + "'", con1);
        SqlCommand myCommand = new SqlCommand(sNavigate(0), con1);
        /SELECT * FROM tblDW2 WHERE [User]='Petrus'
        myReader = myCommand.ExecuteReader();

        while (myReader.Read())
        {
            txtbxDaywords.Text = (myReader["Dayword"].ToString());
        }
        con1.Close();    
        iMainCounter = iDefualt;
        // "Daywords\t" + "\n" + DateTime.Now.ToString();
    }
    public string sNavigate(int iNavNum)
    {
        int iNavigate;

        if (iNavNum != 0)
        {
            iNavigate = iNavNum;    
        }
        else
        {                    
            iNavigate = iDefualt;
        }        
        return "SELECT * FROM (SELECT Dayword, ROW_NUMBER() OVER (ORDER BY Dayword) AS      Rownumber FROM tblDW2 WHERE [User]='Petrus' ) results WHERE results.Rownumber = "+ iNavigate.ToString();
    }    
    protected void btnNext_Click1(object sender, EventArgs e)
    {
        iMainCounter++;    
        con1.Open();
        SqlDataReader myReader = null;
        SqlCommand myCommand = new SqlCommand(sNavigate(iMainCounter), con1);
        myReader = myCommand.ExecuteReader();    
        while (myReader.Read())
        {
            txtbxDaywords.Text = (myReader["Dayword"].ToString());
        }
        con1.Close();
    }
}

如果您没有将计数器存储在某个位置,则在
页面加载
回发

你可以用o

无论如何,如果你不把计数器初始化放在

If(!IsPostBack)
{
    iMainCounter = iDefualt; 
}

每次以这种方式重置它时,您的计数将始终为2,因为您在页面加载事件中设置了它,并且页面加载事件将检查Ispostback,因此它将始终重新初始化为等于iDefault。 因此,您应该在not post back validation中设置iDefault

if(!Page.IsPostBack)
{
   iMainCounter = iDefault;
}

另一个问题是
iMainCounter
未设置为static,因此每个事务都将重新初始化它,因此根据ASP.NET页面生命周期使用
static int iMainCounter=0

当在任何事件中初始化任何变量时,可能是页面加载或任何其他事件。该初始化是特定于用户的 并将在触发事件后立即重新初始化

解决方案:

如果您希望全局保存计数器(不是特定于用户):使用应用程序变量保存计数器值

如果您希望保留特定于某个用户的计数器(对于整个应用程序中的特定用户):请使用会话来保留计数器值

请让我知道,如果你需要一个样本代码,以便我可以提供

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["Incrementer"] == null)
        {
            Session["Incrementer"] = "1";
        }
        else
        {
            int incrementer = Convert.ToInt32(Session["incrementer"].ToString()) + 1;
            Session["incrementer"] = incrementer.ToString();
        }
        Label1.Text = Session["incrementer"].ToString();
    }
}

试着给他打个私人电话。你应该试着把你的问题弄清楚。准备一个“最小的可编译示例”,只需按钮和计数器。private const int不行,它的const,使用private static int多研究一下asp.net的工作原理谢谢,我想这就是我想要做的。但是,有没有办法在递增器开始计数之前将其设置为默认值?例如,我想设置一个日计数器,它每天都会将所有用户的默认起点提前一天移动?然后我想把这一点设置为增量的起点,他们可以从这一点向前和向后导航。