在方法调用期间回发后不保留值

时间:2016-04-08 19:03:27

标签: c# asp.net vb.net variables

我目前正致力于将vb.net桌面应用程序迁移到Web应用程序中,在桌面应用程序中,之前的开发人员已经在

上声明了所有变量

的Form_Load

stdate = VB6.Format(Now, "mmddyyyyhh")
current_month = CInt(Mid(stdate, 1, 2))
current_day = CInt(Mid(stdate, 3, 2))
current_year = CInt(Mid(stdate, 5, 4))
current_hour = CInt(Mid(stdate, 9, 2))
base_year = current_year
this_year = current_year
base_month = current_month
this_month = current_month
txtBaseyear.Text = CStr(base_year)
txtBaseMonth.Text = CStr(base_month)
swyearerror = 0 

然后,他们可以在代码中的任何位置使用这些变量,并保留值。

我的申请

protected void Page_Load(object sender, EventArgs e)
{
    DateTime now = DateTime.Now;
    string format = "MMdyyyyhh";
    string stdate = now.ToString(format);
    Session["stdate"] = stdate;
    int current_month = Convert.ToInt32(now.Month);
    Session["currentmonth"] = current_month;
    int current_day = Convert.ToInt32(now.Day);
    Session["currentday"] = current_day;
    int current_year = Convert.ToInt32(now.Year);
    Session["currentyear"] = current_year;
    int current_hour = Convert.ToInt32(now.Hour);
    Session["currenthour"] = current_hour;
    int base_year = (int)(Session["currentyear"]);
    int this_year = (int)(Session["currentyear"]);
    int base_month = (int)(Session["currentmonth"]);
    int this_month = (int)(Session["currentmonth"]);
    TxtBase.Text = Convert.ToString(base_year);
    TxtBase1.Text = Convert.ToString(base_month);
}

现在,每次文本更改后都有回发时,值会丢失并设置为零,所以我尝试将其存储在会话中但仍然无效。

让我看看我的情况。

1)在第一次加载时,一切都很好。 2)然后用户输入一个项目并点击进入

protected void TxtItem_TextChanged(object sender, EventArgs e)
{
     Calc_Rotation();
     Calc_Best_Before();
}

现在...

public void Calc_Rotation()
{

    switch (current_month) becomes 0 suppose to be 4 
    {
        case 1:
            rotation_month = "A";
            break;
        case 2:
            rotation_month = "B";
            break;
        case 3:
            rotation_month = "G";
            break;
        case 4:
            rotation_month = "J";
            break;
        case 5:
            rotation_month = "K";
            break;
        case 6:
            rotation_month = "L";
            break;
        case 7:
            rotation_month = "N";
            break;
        case 8:
            rotation_month = "P";
            break;
        case 9:
            rotation_month = "S";
            break;
        case 10:
            rotation_month = "W";
            break;
        case 11:
            rotation_month = "Y";
            break;
        case 12:
            rotation_month = "Z";
            break;
    }

    switch (current_hour)  becomes 0 suppose to be the current hour
    {
        case 0:
        case 1:
            rotation_batch = 2;
            break;
        case 2:
        case 3:
            rotation_batch = 4;
            break;
        case 4:
        case 5:
            rotation_batch = 6;
            break;
        case 6:
        case 7:
            rotation_batch = 8;
            break;
        case 8:
        case 9:
            rotation_batch = 10;
            break;
        case 10:
        case 11:
            rotation_batch = 12;
            break;
        case 12:
        case 13:
            rotation_batch = 14;
            break;
        case 14:
        case 15:
            rotation_batch = 16;
            break;
        case 16:
        case 17:
            rotation_batch = 18;
            break;
        case 18:
        case 19:
            rotation_batch = 20;
            break;
        case 20:
        case 21:
            rotation_batch = 22;
            break;
        case 22:
        case 23:
            rotation_batch = 24;
            break;
    }
}

同样适用于Calc_Best_Before

在我尝试了回发解决方案后,我仍然得到0值。

enter image description here

3 个答案:

答案 0 :(得分:0)

如果您使用Session来存储值,则需要确保检查IsPostBack属性。否则,每次页面回发发生时,您将再次初始化值,从而松开状态。

Page.IsPostBack属性基本上表示Page是否第一次访问服务器。

protected void Page_Load(object sender, EventArgs e)
        {
             if (!IsPostBack)
             {

                DateTime now = DateTime.Now;
                string format = "MMdyyyyhh";
                string stdate = now.ToString(format);
                Session["stdate"] = stdate;
                int current_month = Convert.ToInt32(now.Month);
                Session["currentmonth"] = current_month;
                int current_day = Convert.ToInt32(now.Day);
                Session["currentday"] = current_day;
                int current_year = Convert.ToInt32(now.Year);
                Session["currentyear"] = current_year;
                int current_hour = Convert.ToInt32(now.Hour);
                Session["currenthour"] = current_hour;
                int base_year = (int)(Session["currentyear"]);
                int this_year = (int)(Session["currentyear"]);
                int base_month = (int)(Session["currentmonth"]);
                int this_month = (int)(Session["currentmonth"]);
                TxtBase.Text = Convert.ToString(base_year);
                TxtBase1.Text = Convert.ToString(base_month);
           }
}

更新1:

@CodeMan:您需要使用Session来读取函数Calc_Rotation中当前月份的值。您正尝试从变量current_month读取值int并默认为零。

我建议您使用Property来包装Session变量,然后使用代码中的属性。下面,我创建了一个包装器属性CurrentMonth,它封装了在网页中Session的读写操作,在类范围内声明。 Simmilary你也可以为其他变量创建包装器属性。创建属性后,您需要读取Calc_Rotation函数中的属性值。

private int CurrentMonth {
            get {
                var monthfromSession = HttpContext.Current.Session["ClassNameSpace.CurrentMonth"];
                return monthfromSession!= null ? (int)monthfromSession : 0;
            }
                set {
                HttpContext.Current.Session["ClassNameSpace.CurrentMonth"] = value;
             }
        }



public void Calc_Rotation()
{

    switch (this.CurrentMonth)  // it should have correct month value now.
    {
        case 1:
            rotation_month = "A";
            break;
        case 2:
            rotation_month = "B";
            break;
        case 3:
       ....
    }
}

答案 1 :(得分:0)

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
       return;
    DateTime now = DateTime.Now;
    string format = "MMdyyyyhh";
    string stdate = now.ToString(format);
    Session["stdate"] = stdate;
    int current_month = Convert.ToInt32(now.Month);
    Session["currentmonth"] = current_month;
    int current_day = Convert.ToInt32(now.Day);
    Session["currentday"] = current_day;
    int current_year = Convert.ToInt32(now.Year);
    Session["currentyear"] = current_year;
    int current_hour = Convert.ToInt32(now.Hour);
    Session["currenthour"] = current_hour;
    int base_year = (int)(Session["currentyear"]);
    int this_year = (int)(Session["currentyear"]);
    int base_month = (int)(Session["currentmonth"]);
    int this_month = (int)(Session["currentmonth"]);
    TxtBase.Text = Convert.ToString(base_year);
    TxtBase1.Text = Convert.ToString(base_month);
}

答案 2 :(得分:0)

将您的代码放在下面的代码中。如果第一次加载网页,则IsPostBack属性为“false”,并且将执行if条件内的代码。 如果页面加载后回发IsPostBack变为'true',导致程序执行转出if条件。

{{1}}