0

到目前为止,我真的很享受我在 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 功能的服务器,但如果需要,我很乐意发送文件等。

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

4

1 回答 1

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' />";
        }
    }
}
于 2012-07-27T03:25:02.350 回答