2014-01-10 83 views
0

我在C#windows web窗體(在Visual C#2010 Express中設計)中有簡單的應用程序。簡單的Windows web窗體應用程序到asp.net應用程序

只有3個按鈕和3個標籤 - 當我單擊按鈕2時,我在int中的天數(從0到6),然後將此數字切換爲正確的標籤名稱。另外,當我點擊按鈕1或3時,我在所有標籤中都有+1或-1天。

下面是一個例子:

private void button1_Click(object sender, EventArgs e)            
    { 

     NumberDay--; 
     if (NumberDay < 0) NumberDay = 6; 
     switch (NumberDay) 
     { 
      case 0: 
       label1.Text = "Saturday";        
       label2.Text = "Sunday"; 
       label3.Text = "Monday"; 
       break; 
      case 1: 
       label1.Text = "Sunday"; 
       label2.Text = "Monday"; 
       label3.Text = "Tuesday"; 
       break; 
      case 2: 
       label1.Text = " Monday"; 
       label2.Text = "Tuesday"; 
       label3.Text = "Wednesday"; 
       break; 
      case 3: 
       label1.Text = "Tuesday"; 
       label2.Text = "Wednesday"; 
       label3.Text = "Thursday"; 
       break; 
      case 4: 
       label1.Text = "Wednesday"; 
       label2.Text = "Thursday"; 
       label3.Text = "Friday"; 
       break; 
      case 5: 
       label1.Text = "Thursday"; 
       label2.Text = "Friday"; 
       label3.Text = "Saturday"; 
       break; 
      case 6: 
       label1.Text = "Friday"; 
       label2.Text = "Saturday"; 
       label3.Text = "Sunday"; 
       break; 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     button1.Enabled = button3.Enabled = true; 
     DateTime today = DateTime.Now;          
     NumberDay = (int)today.DayOfWeek;        

     switch (NumerDnia) 
     { 
      case 0: 
       label1.Text = "Saturday";        
       label2.Text = "Sunday"; 
       label3.Text = "Monday"; 
       break; 
      case 1: 
       label1.Text = "Sunday"; 
       label2.Text = "Monday"; 
       label3.Text = "Tuesday"; 
       break; 
      case 2: 
       label1.Text = " Monday"; 
       label2.Text = "Tuesday"; 
       label3.Text = "Wednesday"; 
       break; 
      case 3: 
       label1.Text = "Tuesday"; 
       label2.Text = "Wednesday"; 
       label3.Text = "Thursday"; 
       break; 
      case 4: 
       label1.Text = "Wednesday"; 
       label2.Text = "Thursday"; 
       label3.Text = "Friday"; 
       break; 
      case 5: 
       label1.Text = "Thursday"; 
       label2.Text = "Friday"; 
       label3.Text = "Saturday"; 
       break; 
      case 6: 
       label1.Text = "Friday"; 
       label2.Text = "Saturday"; 
       label3.Text = "Sunday"; 
       break; 
     } 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 

     NumberDay++; 
     if (NumberDay > 6) NumberDay = 0; 
     switch (NumberDay) 
     { 
      case 0: 
       label1.Text = "Saturday";        
       label2.Text = "Sunday"; 
       label3.Text = "Monday"; 
       break; 
      case 1: 
       label1.Text = "Sunday"; 
       label2.Text = "Monday"; 
       label3.Text = "Tuesday"; 
       break; 
      case 2: 
       label1.Text = " Monday"; 
       label2.Text = "Tuesday"; 
       label3.Text = "Wednesday"; 
       break; 
      case 3: 
       label1.Text = "Tuesday"; 
       label2.Text = "Wednesday"; 
       label3.Text = "Thursday"; 
       break; 
      case 4: 
       label1.Text = "Wednesday"; 
       label2.Text = "Thursday"; 
       label3.Text = "Friday"; 
       break; 
      case 5: 
       label1.Text = "Thursday"; 
       label2.Text = "Friday"; 
       label3.Text = "Saturday"; 
       break; 
      case 6: 
       label1.Text = "Friday"; 
       label2.Text = "Saturday"; 
       label3.Text = "Sunday"; 
       break; 
     } 
    } 

在正常的C#應用​​程序所有的工作。當我嘗試做同樣在asp.net(視覺快遞2012年VEB)我有一些麻煩:

  1. 當我點擊按鈕2這一切都很好。當我按下按鈕1/3一次我在所有標籤中有錯誤的一天(或者當我按下什麼都沒有發生時)

  2. 當我按下按鈕多次時沒有任何事發生 - 頁面沒有重新加載(?)按鈕點擊,標籤不變。

有人可以幫助我嗎?

+1

是NumberDay是頁面級別的靜態變量嗎? –

+0

你是Asp.net的新手,所以首先是Asp.net的谷歌基礎知識。 Asp.net和Windows應用程序在狀態方面是完全不同的 –

+0

@ user2456849:請提供ASPX代碼,以便我們可以爲您提供幫助。 –

回答

0

如果剛剛宣佈你在頁面級變量「NumberDay」像這樣

int NumberDay; 

,這就是爲什麼它不是維持它的價值,因爲NumberDay得到重置每一個崗位回來。你可以使用ViewState這樣做

private void button1_Click(object sender, EventArgs e)            
{ 

    NumberDay = ViewState["VS_NumberDay"] != null ? Convert.ToInt32(ViewState["VS_NumberDay"]) : 0; 
    NumberDay--;   

    if (NumberDay < 0) NumberDay = 6; 

    ViewState["VS_NumberDay"] = NumberDay; 

    switch (NumberDay) 
    { 
     case 0: 
      label1.Text = "Saturday";        
      label2.Text = "Sunday"; 
      label3.Text = "Monday"; 
      break; 
     case 1: 
      label1.Text = "Sunday"; 
      label2.Text = "Monday"; 
      label3.Text = "Tuesday"; 
      break; 
     case 2: 
      label1.Text = " Monday"; 
      label2.Text = "Tuesday"; 
      label3.Text = "Wednesday"; 
      break; 
     case 3: 
      label1.Text = "Tuesday"; 
      label2.Text = "Wednesday"; 
      label3.Text = "Thursday"; 
      break; 
     case 4: 
      label1.Text = "Wednesday"; 
      label2.Text = "Thursday"; 
      label3.Text = "Friday"; 
      break; 
     case 5: 
      label1.Text = "Thursday"; 
      label2.Text = "Friday"; 
      label3.Text = "Saturday"; 
      break; 
     case 6: 
      label1.Text = "Friday"; 
      label2.Text = "Saturday"; 
      label3.Text = "Sunday"; 
      break; 
    } 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    button1.Enabled = button3.Enabled = true; 
    DateTime today = DateTime.Now;          
    NumberDay = (int)today.DayOfWeek;        
    ViewState["VS_NumberDay"] = NumberDay; 
    switch (NumerDnia) 
    { 
     case 0: 
      label1.Text = "Saturday";        
      label2.Text = "Sunday"; 
      label3.Text = "Monday"; 
      break; 
     case 1: 
      label1.Text = "Sunday"; 
      label2.Text = "Monday"; 
      label3.Text = "Tuesday"; 
      break; 
     case 2: 
      label1.Text = " Monday"; 
      label2.Text = "Tuesday"; 
      label3.Text = "Wednesday"; 
      break; 
     case 3: 
      label1.Text = "Tuesday"; 
      label2.Text = "Wednesday"; 
      label3.Text = "Thursday"; 
      break; 
     case 4: 
      label1.Text = "Wednesday"; 
      label2.Text = "Thursday"; 
      label3.Text = "Friday"; 
      break; 
     case 5: 
      label1.Text = "Thursday"; 
      label2.Text = "Friday"; 
      label3.Text = "Saturday"; 
      break; 
     case 6: 
      label1.Text = "Friday"; 
      label2.Text = "Saturday"; 
      label3.Text = "Sunday"; 
      break; 
    } 
} 

private void button3_Click(object sender, EventArgs e) 
{ 

    NumberDay = ViewState["VS_NumberDay"] != null ? Convert.ToInt32(ViewState["VS_NumberDay"]) : 0; 
    NumberDay++;   
    if (NumberDay > 6) NumberDay = 0; 

    ViewState["VS_NumberDay"] = NumberDay; 

    switch (NumberDay) 
    { 
     case 0: 
      label1.Text = "Saturday";        
      label2.Text = "Sunday"; 
      label3.Text = "Monday"; 
      break; 
     case 1: 
      label1.Text = "Sunday"; 
      label2.Text = "Monday"; 
      label3.Text = "Tuesday"; 
      break; 
     case 2: 
      label1.Text = " Monday"; 
      label2.Text = "Tuesday"; 
      label3.Text = "Wednesday"; 
      break; 
     case 3: 
      label1.Text = "Tuesday"; 
      label2.Text = "Wednesday"; 
      label3.Text = "Thursday"; 
      break; 
     case 4: 
      label1.Text = "Wednesday"; 
      label2.Text = "Thursday"; 
      label3.Text = "Friday"; 
      break; 
     case 5: 
      label1.Text = "Thursday"; 
      label2.Text = "Friday"; 
      label3.Text = "Saturday"; 
      break; 
     case 6: 
      label1.Text = "Friday"; 
      label2.Text = "Saturday"; 
      label3.Text = "Sunday"; 
      break; 
    } 
} 
+0

偉大的工作thx :) – user2456849

相關問題