获取网格视图中已选中行的textBox值

时间:2013-12-29 14:22:36

标签: c# asp.net gridview textbox

我遇到一些问题,从文本框中获取文本框,其中复选框已标记为检查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;
        }
    }

假设我得到prodID 1有50个单位,prodID 2有77个单位,prodID 3有90个单位。当我循环通过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);
            }
        } 

3 个答案:

答案 0 :(得分:1)

你可以这样做:

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

其中GV是gridview Id 和复选框是0列,您可能想要获得的值是1列

答案 1 :(得分:1)

这是因为您选择了复选框,每行都会覆盖变量“quantity”。如果要存储多个值,则需要使用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 :(得分:1)

您可以使用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"。< / p>