【问题标题】:Getting textBox value of checked row in grid view在网格视图中获取选中行的文本框值
【发布时间】: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


【解决方案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"

【讨论】:

  • 你能帮我检查编辑的部分吗?当我尝试访问数量时,它给了我输入字符串格式不正确的错误消息。为什么会这样?
  • 我已经编辑了我的答案,你必须转换 tempList[key] 而不是数量变量。您还必须验证并确保文本框应仅包含整数值。
  • 我认为 tempList[key] 包含 prodID 和数量?程序如何知道要与哪一个进行比较?嗯,它不起作用。报错信息还是一样
  • 刚刚再次编辑了我的答案并添加了一些解释,希望对您有所帮助。
  • 哦,是的,我在检索时连接了我的数量。对不起我的错误
【解决方案2】:

这是因为您一直为选中复选框的每一行覆盖变量“数量”。如果要存储多个值,则需要使用List&lt;string&gt; 来存储选中复选框的每一行的数量。

如下所示。注意:我没有测试过代码。

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

【讨论】:

  • 这意味着我需要一个嵌套的 for 循环来获取 prodID 和数量?你能给我举几个例子吗?
  • 哦,我不应该放置嵌套循环,因为它会给我一个索引超出范围异常的错误消息。谢谢它的工作原理!
  • 很高兴它可以工作......你也可以使用@ekad提到的字典......它是一个更好的解决方案......
  • 但是,当我尝试比较数量值时,它告诉我输入字符串的格式不正确,尽管我已经尝试过:Convert.ToInt32(quantity)。你能帮我检查一下编辑过的部分并帮我指出哪里出了问题吗?
  • 你是在使用@ekad 提到的字典还是我的解决方案?
【解决方案3】:

你可以这样做:

        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 是网格视图 ID 并且复选框是 0 列,您可能想要获得的值是 1 列

【讨论】:

  • 我设法得到了检查的行索引,但没有得到文本框的值。您的 Cells[1] 表示 textBox 列?
  • 或者我应该添加另一个列表来存储数量,就像我为 prodID 所做的那样?
  • cells[i] 是当前行的第 i 列单元格
  • 但它不是文本框。应该如何将其转换为 TextBox?
  • 我编辑了我的答案,向您展示如何将单元格转换为文本框
猜你喜欢
  • 2019-03-09
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 2015-01-29
  • 2014-11-06
  • 2019-03-07
  • 1970-01-01
相关资源
最近更新 更多