【发布时间】:2013-12-29 14:22:36
【问题描述】:
我在从一行中的文本框中获取文本时遇到了一些问题,该复选框被标记为在 gridview 中选中。单击按钮时,它应该获取 prodID 的选中行索引和文本框中的文本数量:
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;
}
}
假设我有 50 个单位的 prodID 1,有 77 个单位的 prodID 2,有 90 个单位的 prodID 3。当我通过 tempList 循环时,这是我应该得到的结果:
1 50units, 2 77units, 390units
但是,代码不会从每个产品的文本框中独立获取数量。这是我得到的结果:
1 90units, 2 90units, 3 90units
它只是简单地获取列表中最后一个产品的数量。我想知道有没有办法解决这个问题?提前致谢。
编辑部分:
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);
}
}
【问题讨论】:
-
你是否选中了多行中的复选框?
标签: c# asp.net gridview textbox