Getting the textBox value of a checked string in grid mode

I'm having some problem getting text from a text box in a row that has been checked in a gridview. When the button is pressed, it should get the check row index for prodID and the amount, which is the text from the text field:

protected void lbnConfirm_Click(object sender, EventArgs e)
    {
        string quantity = "" , prodID = "";
        foreach (RepeaterItem item in Repeater1.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                Panel pnl = item.FindControl("pBody1") as Panel;
                GridView gv = pnl.FindControl("gvProduct") as GridView;
                foreach (GridViewRow gr in gv.Rows)
                {
                    CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
                    if (cb.Checked)
                    {
                        //Get the productID which set as DataKeyNames for selected row index
                        prodID = gv.DataKeys[gr.RowIndex].Value.ToString();

                        var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
                        if (tbQuantity != null)
                        {
                            quantity = tbQuantity.Text;
                        }
                        tempList.Add(prodID);
                    }  
                }
            }
        }
        for (int i = 0; i < tempList.Count; i++)
        {    
            //Testing
            lblTest.Text += tempList[i] + " " + quantity;
        }
    }

Say I got prodID 1 with 50 units, prodID 2 with 77 units, prodID 3 with 90 units. When I loop through tempList, this is the result I should get:

1 50units, 2 77units, 390units

However, the codes do not receive the amount from the text box for each product independently. Here is the result I get:

1 90units, 2 90units, 3 90units

Just just get the amount of the last product listed. I wonder if there is a way to fix this? Thanks in advance.

Edited Part:

 foreach (string key in tempList.Keys)
        {
            packagesNeeded = 1;
            unitQty = prodPackBLL.getUnitQtySPU(tempList[key]);
            lblTest.Text += key + " " + tempList[key];
            if (Convert.ToInt32(quantity) < (packagesNeeded * unitQty))
            {
                //Pop up message
                Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript", "alert(\"Insufficient storage\");", true);
            }
        } 
+4
3

Dictionary prodID quantity. :

protected void lbnConfirm_Click(object sender, EventArgs e)
{
    Dictionary<string, string> tempList = new Dictionary<string, string>();
    string quantity = "", prodID = "";
    foreach (RepeaterItem item in Repeater1.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Panel pnl = item.FindControl("pBody1") as Panel;
            GridView gv = pnl.FindControl("gvProduct") as GridView;
            foreach (GridViewRow gr in gv.Rows)
            {
                CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
                if (cb.Checked)
                {
                    //Get the productID which set as DataKeyNames for selected row index
                    prodID = gv.DataKeys[gr.RowIndex].Value.ToString();

                    var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
                    if (tbQuantity != null)
                    {
                        quantity = tbQuantity.Text;
                    }
                    tempList.Add(prodID, quantity);
                }
            }
        }
    }

    foreach (string key in tempList.Keys)
    {
        packagesNeeded = 1;
        unitQty = prodPackBLL.getUnitQtySPU(key);
        lblTest.Text += key + " " + tempList[key];
        if (Convert.ToInt32(tempList[key]) < (packagesNeeded * unitQty))
        {
            //Pop up message
            Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript", "alert(\"Insufficient storage\");", true);
        }
    } 
}

, tempList["1"] "50", tempList["2"] "77", tempList["3"] "90".

+2

, "" , . , List<string> , .

- . . .

protected void lbnConfirm_Click(object sender, EventArgs e)
{
    List<string> quantity = new List<string>(); 
    prodID = "";
    foreach (RepeaterItem item in Repeater1.Items)
    {
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Panel pnl = item.FindControl("pBody1") as Panel;
            GridView gv = pnl.FindControl("gvProduct") as GridView;
            foreach (GridViewRow gr in gv.Rows)
            {
                CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
                if (cb.Checked)
                {
                    //Get the productID which set as DataKeyNames for selected row index
                    prodID = gv.DataKeys[gr.RowIndex].Value.ToString();

                    var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
                    if (tbQuantity != null)
                    {
                        quantity.Add(tbQuantity.Text);
                    }
                    tempList.Add(prodID);
                }  
            }
        }
    }
    for (int i = 0; i < tempList.Count; i++)
    {    
        //Testing
        lblTest.Text += tempList[i] + " " + quantity[i];
    }
}
+2

- :

        foreach (DataGridViewRow item in GV.Rows)
        {
            if (Convert.ToBoolean(item.Cells[0].Value) == true)
                //here you get the rowcell value :
                string val = item.Cells[1].Value.ToString();
                //If you want to convert to a textbox :
                TextBox textBox = (TextBox)item.Cells[1].Value;
        }

where GV is the gridview identifier and the checkbox is column 0, and the value you want to get is 1 column

+1
source

All Articles