与IsPostBack有一个小问题(ASP.NET,C#)

时间:2012-07-27 03:08:55

标签: c# asp.net

到目前为止,我真的很享受使用C#的ASP.NET学习经验:)。我只是在理解我的代码实现方面的IsPostBack函数时遇到了麻烦。我在这里看到了一些关于IsPostBack的问题,但我对我的特定实现提出了更多“一般化”的建议。

应用程序相对简单 - 从下拉菜单中选择字体并在文本框中键入一些文本。按显示屏时,将根据您选择的字体选项显示文本。我有这个工作得很好,它正在尝试实现IsPostBack功能,所以当我尝试在文本框中输入其他内容时,之前提交的文本不会显示。我已经尝试改变调用我的FontsList()方法的位置,但这不起作用 - 我得到一个Null Reference错误(我知道原因)。

这是我编译的“代码隐藏”/ 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功能的服务器,但如果需要,我很乐意发送文件等。

非常感谢任何人的帮助/反馈,一如既往:)。

1 个答案:

答案 0 :(得分: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' />";
        }
    }
}