2012-07-27 90 views
0

到目前爲止,我真的很享受ASP.NET和C#的學習體驗:)。我只是無法理解與我的代碼實現相關的IsPostBack函數。我在這裏看到了關於IsPostBack的一些問題,但是在爲我的特定實現提供了更多的「一般化」建議之後。IsPostBack(ASP.NET,C#)存在一個小問題

該應用程序相對簡單 - 您可以從下拉菜單中選擇一種字體,然後在文本框中鍵入一些文本。當您按下顯示器時,您的文本將根據您選擇的字體選項顯示。我已經得到了這個工作很好,它試圖實現IsPostBack功能,所以當我嘗試在文本框中輸入其他內容時,以前提交的文本未顯示。我試着改變我的FontsList()方法被調用的地方,但這不起作用 - 我得到一個空引用錯誤(我知道爲什麼)。

這裏是「代碼隱藏」/C#代碼我已經編譯:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class _Default : System.Web.UI.Page 
{ 

    List<String> folderNames; 
    List<String> filePrefixes; 
    List<String> fileSuffixes; 

    protected void FontsList() 
    { 


     folderNames = new List<String> {"cartoon", "copperDeco", "decoTwoTone", "embroidery", "fancy", "goldDeco", "green", 
             "greenChunky", "ice", "letsFaceIt", "lights", "peppermintSnow", "polkadot", "rainbow", "seaScribe", 
             "shadow", "snowflake", "teddy", "tiger", "Victorian", "water", "wood", "zebra"}; 

     filePrefixes = new List<String> {"alphabet_" + "", "copperdeco-", "", "embroidery-", "art_", "golddeco-", "", "109", "ice", 
             "faceoff-", "", "peppermint-", "polkadot-", "", "", "shad_", "snowflake-", "alphabear", "", "vic", 
             "wr_", "wood", "zebra-"}; 

     fileSuffixes = new List<String> {"s", "", "4", "", "", "", "", "", "", "", "1", "", "", "", "", "", "", "" + "smblue", "", 
             "", "", "", ""}; 


    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     FontsList(); 
     if (!IsPostBack) 
     { 
      //FontsList(); 
      foreach (String s in folderNames) 
      { 
       DropDownList.Items.Add(s); 
      } 
     } 

    } 

    protected void submitDisplay_Click(object sender, EventArgs e) 
    { 
     int index = folderNames.IndexOf(DropDownList.Text); //drop down box 

     foreach (Char c in textBox.Text) 
     { 
      if(c == ' ') 
      { 
       displayText.InnerHtml += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; 
      } 
      else 
      { 
      displayText.InnerHtml += "<img src = 'Alphabets/" + folderNames[index] + "/" + filePrefixes[index] + c + fileSuffixes[index] + ".gif' />"; 
      } 
     } 
    } 
} 

不幸的是我沒有訪問具有ASP.NET功能的服務器,但很高興地發送文件等如果需要的話。

任何人的幫助/反饋非常感謝,一如既往:)。

+0

您的aspx文件是什麼樣子的? – 2012-07-27 03:17:30

回答

2

在ASP .NET中,控件會自動將其狀態存儲在ViewState對象中。當頁面被重新發布時,displayText控件仍然具有前一次點擊的價值,並且您正在將新圖像添加到它。您需要在添加新值之前清除之前的數據:

protected void submitDisplay_Click(object sender, EventArgs e) 
{ 
    displayText.InnerHtml = ""; 

    int index = folderNames.IndexOf(DropDownList.Text); //drop down box 

    foreach (Char c in textBox.Text) 
    { 
     if(c == ' ') 
     { 
      displayText.InnerHtml += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; 
     } 
     else 
     { 
      displayText.InnerHtml += "<img src = 'Alphabets/" + folderNames[index] + "/" + filePrefixes[index] + c + fileSuffixes[index] + ".gif' />"; 
     } 
    } 
} 
+0

我應該知道更好的笑話,重置innerHtml爲空字符串,然後添加字體「圖像」後,每次點擊似乎是最好的解決方案。謝謝堆:)。 – Rob 2012-07-27 03:39:42

+0

希望你也已經知道這一點:頁面指令中的EnableViewState =「false」也會禁用視圖狀態的隱藏字段。 – Shiham 2012-07-27 04:30:16