不能否定并避免文本框中的字母输入

时间:2016-04-20 12:51:13

标签: 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();
    }

我想要发生的是避免字母成为用户的输入。或类似的东西将被默认为&#34; 1&#34;如果他们这样做。

2 个答案:

答案 0 :(得分:3)

使用if语句检查

Application

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

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 
   }

为避免文本输入到字段中,在textBox的Text Changed事件中,您应该在此if语句中添加另一个条件,以及作为out的整数变量

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 
   }

答案 1 :(得分:1)

设法解决它。我添加了

int.Parse(txtQuantity.Text)<1)

到我的代码的第一行。