c# - 不能为负数并避免在文本框中输入字母

标签 c# asp.net negative-number product-quantity

这是我的 txtProductQuantity_TextChange 的完整代码:

protected void txtProductQuantity_TextChanged(object sender, EventArgs e)
    {
        TextBox txtQuantity = (sender as TextBox);

        //int tempForTryParse = 0;
        //if (!int.TryParse(txtQuantity.Text, out tempForTryParse))
        //{
        //    txtQuantity.Text = txtQuantity.Text.Substring(0, txtQuantity.Text.Length - 1);
        //}

        DataListItem currentItem = (sender as TextBox).NamingContainer as DataListItem; // getting current item on where user wants to add or remove
        HiddenField ProductID = currentItem.FindControl("hfProductID") as HiddenField;
        Label lblAvailableStock = currentItem.FindControl("lblAvailableStock") as Label;

        int tempInt = 0;
        if (txtQuantity.Text == string.Empty || txtQuantity.Text == "0" || txtQuantity.Text == "1" || double.Parse(txtQuantity.Text) < 0 || !int.TryParse(txtQuantity.Text, out tempInt))
        {
            txtQuantity.Text = "1"; //default value is 1, no action 
        }
        else
        {
            if (Session["MyCart"] != null) // not null means user has added a product in the shopping cart
            {
                if (Convert.ToInt32(txtQuantity.Text) <= Convert.ToInt32(lblAvailableStock.Text)) // check if quantity is greater than available stock
                {
                    DataTable dt = (DataTable)Session["MyCart"]; // if quantity is less than the available stock, go inside the code

                    DataRow[] rows = dt.Select("ProductID = '" + ProductID.Value + "'"); // select specific row depending on the product id

                    int index = dt.Rows.IndexOf(rows[0]);

                    dt.Rows[index]["ProductQuantity"] = txtQuantity.Text; // putting the value in the txtQuantityTextbox. changing the product quantity in the data table

                    Session["MyCart"] = dt; // add updated value to datatable
                }
                else // error if quntity is greater than available stock
                {
                    lblAvailableStockAlert.Text = " Alert: product buyout should not be more than the available stock!";
                    txtQuantity.Text = "1"; // automatically change the quantity back to 1.
                }

            }
        }
        UpdateTotalBill();
    }

我想要发生的是避免字母成为用户的输入。或者类似的东西如果这样做的话将默认为“1”。

最佳答案

使用if语句来检查

if(txtQuantity.Text.StartsWith("-")
{
    //Code if negative
}

您还可以将字符串解析为数字,然后检查它是否为负数

if(int.Parse(txtQuantity.Text) < 0)
{
    //Code if negative
}

因此,在您的代码上下文中,您可以像这样使用它

if (txtQuantity.Text == string.Empty || txtQuantity.Text == "0" || txtQuantity.Text == "1" || txtQuantity.Text.StartsWith("-"))
   {
        txtQuantity.Text = "1"; //default value is 1, no action 
   }

if (txtQuantity.Text == string.Empty || txtQuantity.Text == "0" || txtQuantity.Text == "1" || int.Parse(txtQuantity.Text) < 0)
   {
        txtQuantity.Text = "1"; //default value is 1, no action 
   }

为了避免在字段中输入文本,在文本框的“文本更改”事件中,您应该在此 i​​f 语句中添加另一个条件,以及充当输出的整数变量

int tempInt = 0;
bool parsed = int.TryParse(txtQuantity.Text, out tempInt);
if (txtQuantity.Text == string.Empty || txtQuantity.Text == "0" || txtQuantity.Text == "1" || tempInt < 0 || !parsed)
   {
        txtQuantity.Text = "1"; //default value is 1, no action 
   }

关于c# - 不能为负数并避免在文本框中输入字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36744395/

相关文章:

c# - 如何将 `System.Dynamic` 添加到我的项目中?

c# - 是否有一些调试工具可以在平面表格中显示结果

c# - 如何确定 CultureInfo 实例是否支持拉丁字符

java - 如何使用递归在Java中创建负斐波那契数列?

java - Java 中具有负字节值的 >> 和 >>> 运算符

c# - 在构建强大的文件导入器时,您的第一个 TDD 测试是什么?

c# - 在 Visual Studio 中更新 git commit 上的程序集版本

c# - 从另一个线程在窗体上添加控件

asp.net - 删除页面的水平滚动条

c - Printf 结果为负