简体   繁体   中英

How can i change value from textbox in row in gridview?

I am getting value from textbox in gridview and trying to change the value with two different buttons. (increase and decrease buttons) First click is working than not working.

I was checking browser's console for getting information and i am getting this information: "XHR finished loading: POST" Do you have an idea why button click works only once?

Here is my code for increase:

public void btn_increase_Click(object sender, EventArgs e)
{
   foreach(GridViewRow row in gridview1.Rows)
   {
     Textbox quantity = (Textbox)row.FindControl("txt_quantity");
     int input_quantity = Convert.ToInt32(quantity.Text);
     if(input_quantitiy >= 0)
     {
       quantity.Text = Convert.ToString(input_quantity + 1);
     }
   }
}

UPDATED

If you have copied that code exactly from your program there are a few issues you could try fixing that might help:

  • input_quantity in the if() statement is spelt incorrectly.
  • Swap the = and > around in the if statement, currently isn't checking for greater than or equal too, but is a lambda expression.

Also if you know the ID of the TextBox, and only need to increase/decrease one TextBox, is there any reason to iterate through the GridView? You could achieve the same thing as follow:

            TextBox quantity = txt_quantity;
            int input_quantity = Convert.ToInt32(quantity.Text);
            if (input_quantity >= 0)
            {
                quantity.Text = Convert.ToString(input_quantity + 1);
            }

I found a solution and it's very simple. If your button click is not working and getting "XHR finished loading: POST" error on browser. Check your Page_Load and be sure bind gridview in if page is not postback like that:

if(!IsPostBack)
{
   gridview.DataSource = dataset.Tables[0];
   gridview.DataBind();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM