2016-04-20 15 views
2

hier ist mein ganzer Code fro die txtProductQuantity_TextChange vermeiden:kann nicht negativ sein und Buchstabeneingabe auf Textbox

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

, was ich will Buchstaben geschehen ist zu vermeiden, eine Eingabe vom Benutzer zu sein. oder etwas wie es wird auf "1" voreingestellt, wenn sie es tun.

Antwort

3

Verwenden Sie eine if-Anweisung

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

überprüfen Sie könnten auch die Zeichenfolge als Zahl analysieren und dann prüfen, ob es negativ ist

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

So im Zusammenhang mit Ihrem Code, den Sie verwenden könnten es mag dieses

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

oder

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 
    } 

Text Um zu vermeiden, in das Feld eingegeben, auf dem Text Changed-Ereignis für den textBox Sie weitere Kriterien in diese if-Anweisung sowie ein Integer-Variable zu handeln als aus

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 
    } 
+0

danke mate. Irgendeine Idee, wie man vermeidet, Buchstaben darauf einzutragen? weil es mir einen Fehler gibt, wenn ich es tue. –

+0

@PaoloDuhaylungsod für diese Situation bearbeitet –

+0

Ich habe meinen ganzen Code gezeigt. oder kann es so sein, als ob es auf den Wert 1 gesetzt wäre, wenn sie einen Brief eingegeben hätten? Danke –

1

sollten hinzufügen gelungen, es zu lösen.i hinzugefügt

int.Parse(txtQuantity.Text)<1) 

in die erste Zeile meines Codes.