C# 获取网格视图中选中行的文本框值

C# 获取网格视图中选中行的文本框值,c#,asp.net,gridview,textbox,C#,Asp.net,Gridview,Textbox,在gridview中复选框被标记为check的行中,我从textbox获取文本时遇到一些问题。单击按钮时,应获取prodID的选中行索引和数量,即文本框中的文本: protected void lbnConfirm_Click(object sender, EventArgs e) { string quantity = "" , prodID = ""; foreach (RepeaterItem item in Repeater1.Items)

在gridview中复选框被标记为check的行中,我从textbox获取文本时遇到一些问题。单击按钮时,应获取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;
        }
    }
但是,这些代码并不能独立地从文本框中获取每种产品的数量。以下是我得到的结果:

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);
            }
        } 
foreach(templast.Keys中的字符串键)
{
所需包装=1;
unitQty=prodPackBLL.getUnitQtySPU(模板列表[key]);
lblTest.Text+=key+“”+templast[key];
if(转换为32(数量)<(所需包装*单位数量))
{
//弹出消息
Page.ClientScript.RegisterStartupScript(GetType(),“UserDialogScript”,“警报(\“存储不足\”;),true);
}
} 

您可以执行以下操作:

        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列,这是因为您一直在为选中复选框的每一行覆盖变量“数量”。如果要存储多个值,则需要使用
List
存储选中复选框的每行的数量

像下面这样。注意:我还没有测试代码

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];
    }
}
受保护的无效LBN确认\u单击(对象发送方,事件参数e)
{
列表数量=新列表();
prodID=“”;
foreach(Repeater1.Items中的RepeaterItem项)
{
如果(item.ItemType==ListItemType.item | | item.ItemType==ListItemType.AlternatingItem)
{
Panel pnl=item.FindControl(“pBody1”)作为面板;
GridView gv=pnl.FindControl(“gvProduct”)作为GridView;
foreach(gv.行中的GridViewRow gr)
{
复选框cb=(复选框)gr.Cells[0]。FindControl(“cbCheckRow”);
如果(cb.选中)
{
//获取设置为选定行索引的DataKeyName的productID
prodID=gv.DataKeys[gr.RowIndex].Value.ToString();
var tbQuantity=gr.FindControl(“tbQuantity”)作为文本框;
如果(tbQuantity!=null)
{
数量.Add(tbQuantity.Text);
}
添加(prodID);
}  
}
}
}
for(int i=0;i
您可以使用将每个
prodID
与其对应的
数量进行配对。请尝试以下代码:

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);
        }
    } 
}
受保护的无效LBN确认\u单击(对象发送方,事件参数e)
{
Dictionary templast=新字典();
字符串数量=”,prodID=”;
foreach(Repeater1.Items中的RepeaterItem项)
{
如果(item.ItemType==ListItemType.item | | item.ItemType==ListItemType.AlternatingItem)
{
Panel pnl=item.FindControl(“pBody1”)作为面板;
GridView gv=pnl.FindControl(“gvProduct”)作为GridView;
foreach(gv.行中的GridViewRow gr)
{
复选框cb=(复选框)gr.Cells[0]。FindControl(“cbCheckRow”);
如果(cb.选中)
{
//获取设置为选定行索引的DataKeyName的productID
prodID=gv.DataKeys[gr.RowIndex].Value.ToString();
var tbQuantity=gr.FindControl(“tbQuantity”)作为文本框;
如果(tbQuantity!=null)
{
数量=tbQuantity.Text;
}
添加(prodID,数量);
}
}
}
}
foreach(templast.Keys中的字符串键)
{
所需包装=1;
unitQty=prodPackBLL.getUnitQtySPU(键);
lblTest.Text+=key+“”+templast[key];
if(转换为32(模板列表[key])<(需要的包装*单位数量))
{
//弹出消息
Page.ClientScript.RegisterStartupScript(GetType(),“UserDialogScript”,“警报(\“存储不足\”;),true);
}
} 
}

基于上面的示例,
templast[“1”]
将生成
“50”
templast[“2”
将生成
“77”
templast[“3”]
将生成
“90”

我设法获得了选中的行索引,但没有得到文本框值。您的单元格[1]表示文本框列?或者我是否应该添加另一个列表来存储数量,就像我为prodID所做的那样?单元格[I]是当前行中的第I列单元格,但它不是文本框。如何将其转换为文本框?我编辑了我的answear,向您展示了如何将单元格转换为文本框您是否选择了多行中的复选框?这意味着我需要一个嵌套的for循环来获取prodID和quantity?你能给我看一些例子吗?哦,是的,我不应该放嵌套循环,因为它给我一个索引越界异常的错误消息。谢谢,这很有效!很高兴它起作用了。。。。你也可以使用@ekad提到的字典。。。这是一个更好的解决方案…但是,当我尝试比较数量值时,它告诉我输入字符串的格式不正确,尽管我已经尝试过:Convert.ToInt32(数量)。你能帮我检查编辑的部分并指出哪里出了问题吗?你是在使用@ekad提到的字典还是我的解决方案?你能帮我检查编辑的部分吗?当我试图访问数量时,它会给我一条错误消息,输入字符串的格式不正确。为什么会这样?我已经编辑了我的答案,你必须转换tempList[key]而不是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);
        }
    } 
}