I am having some problem for getting the text from textbox in a row which the checkbox was marked check in gridview. When button on click, it supposed to get the checked row index for prodID and the quantity which is the text from textbox:
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;
}
}
Let's say I got prodID 1 with 50 units, prodID 2 with 77 units, prodID 3 with 90 units. When I loop thru the tempList, this is the result which I supposed to get:
1 50units, 2 77units, 390units
However, the codes does not get the quantity from the textbox for each product independently. Here is the result which I get:
1 90units, 2 90units, 3 90units
It just simply get the quantity of last product in the list. I wonder is there any way to fix this? Thanks in advance.
Edited Portion:
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);
}
}
you can do something like :
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 Id and checkbox is the 0 column and the value you might want to get is the 1 column
It is because you keep overriding variable "quantity" for each row you have the checkbox selected. If you want to store multiple values you need to use List<string>
to store the quantities for each row where the checkbox is selected.
Something like below. Note: I haven't tested the code.
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];
}
}
You can use Dictionary to pair each prodID
with its corresponding quantity
. Try this code:
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);
}
}
}
Based on your examples above, tempList["1"]
will produce "50"
, tempList["2"]
will produce "77"
, and tempList["3"]
will produce "90"
.
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.