0

私の Web サイトは ASP.NET 2.0 と C# で構築されています。ユーザーがコードを入力する tbCode というテキストボックスがあります。ボタンのクリックで、コードビハインドの複数の値に対して入力された値をチェックしようとしています。

これは、これまでの私のマークアップです。

 protected void btUpdate_Click(object sender, EventArgs e)
{


    if ((this.tbcode.Text.Trim().ToUpper() != "AB12") ||(this.tbcode.Text.Trim().ToUpper() != "DE14") || (this.tbcode.Text.Trim().ToUpper() != "XW16"))
    {
        lbmessage.Text = "Invalid Promo code. Please enter again";
    }
    else if ((this.tbcode.Text.Trim().ToUpper() == "AB12") || (this.tbcode.Text.Trim().ToUpper() == "DE14") || (this.tbcode.Text.Trim().ToUpper() == "XW16"))
    {
        Order.Shipping.Cost = 0;
        this.lShipping.Text = Order.Shipping.Cost.ToString("c");
        this.lSubtotal.Text = Order.Subtotal.ToString("c");
        this.lTotal.Text = Order.TotalCost.ToString("c");
        Order.PomoCode = this.tbcode.Text.Trim().ToUpper();
        lbmessage.Text = "Promo Code Applied.";
    }
    else
    {
        this.lShipping.Text = Order.Shipping.Cost.ToString("c");
        this.lSubtotal.Text = Order.Subtotal.ToString("c");
        this.lTotal.Text = Order.TotalCost.ToString("c");

    }

}

ボタンを押すと、常に無効なコードと表示されます。どこで間違いを犯しているのかわかりません。3 ではなく 1 つの値だけをチェックしている場合は、完全に機能します。

感謝します

4

4 に答える 4

2

Here is likely what you wanted to do:

protected void btUpdate_Click(object sender, EventArgs e)
{
    string tbcodeValue = this.tbcode.Text.Trim().ToUpper();

    string[] validCodes = new string[] { "AB12", "DE14", "XW16" };
    if (!validCodes.Contains(tbcodeValue))
    {
        lbmessage.Text = "Invalid Promo code. Please enter again";
    }
    else 
    {
        Order.Shipping.Cost = 0;
        this.lShipping.Text = Order.Shipping.Cost.ToString("c");
        this.lSubtotal.Text = Order.Subtotal.ToString("c");
        this.lTotal.Text = Order.TotalCost.ToString("c");
        Order.PomoCode = tbcodeValue;
        lbmessage.Text = "Promo Code Applied.";
    }
}

First off, you were calling this.tbcode.Text.Trim().ToUpper() all over the place. That really clutters up your code and makes it hard to read. Assigning that to a variable not only makes the code cleaner, but avoids performing all of those string manipulation functions over and over.

Next, it appears that your intent is to say, "if the textbox value isn't any of these values, run some code saying it's invalid. The easiest way to do that is to put all of the valid values into a container of some sort and see if it contains the value you're interested in. Your next block of code is basically for, "if it is one of the valid values". So if it does contain the string then it is valid. As for your else, I couldn't figure out what the intent of it was. Either the string is invalid, or it's valid. I don't see any third case there, so I just removed it.

于 2012-10-16T19:55:29.127 に答える
1

これを試して:

 if ((this.tbcode.Text.Trim().ToUpper() != "AB12") && (this.tbcode.Text.Trim().ToUpper() != "DE14") && (this.tbcode.Text.Trim().ToUpper() != "XW16"))
    {
        lbmessage.Text = "Invalid Promo code. Please enter again";
    }
    else if ((this.tbcode.Text.Trim().ToUpper() == "AB12") || (this.tbcode.Text.Trim().ToUpper() == "DE14") || (this.tbcode.Text.Trim().ToUpper() == "XW16"))
    {
        Order.Shipping.Cost = 0;
        this.lShipping.Text = Order.Shipping.Cost.ToString("c");
        this.lSubtotal.Text = Order.Subtotal.ToString("c");
        this.lTotal.Text = Order.TotalCost.ToString("c");
        Order.PomoCode = this.tbcode.Text.Trim().ToUpper();
        lbmessage.Text = "Promo Code Applied.";
    }
    else
    {
        this.lShipping.Text = Order.Shipping.Cost.ToString("c");
        this.lSubtotal.Text = Order.Subtotal.ToString("c");
        this.lTotal.Text = Order.TotalCost.ToString("c");

    }
于 2012-10-16T19:39:12.700 に答える
1

最初の if ステートメントで || を && に変更する必要があります。そうしないと、常にそのブロックに陥ります。

于 2012-10-16T19:37:05.393 に答える
0

switch;case; を使用することもできます。ブロック。

String testtext = this.tbcode.Text.Trim().ToUpper();

switch(testtext)
{
    case "AB12":
        // Do stuff for this case
        break;
    case "Text2":
        // Do stuff for this case
        break;
    default:
        // anything that fails all above tests goes here
        break;
}

それぞれのケースの後で必ずブレーク、リターン、または続行してください。そうしないと、コンパイル エラーが発生します。また、デフォルトは最後にする必要があります。

于 2012-10-16T20:32:26.673 に答える