C# 基于文本框值在gridview行上显示错误消息

C# 基于文本框值在gridview行上显示错误消息,c#,asp.net,gridview,dictionary,textbox,C#,Asp.net,Gridview,Dictionary,Textbox,我尝试这样做,如果在gridview的某些行上标记了复选框,我将检查用户输入是否超过存储级别。如果超过,我将使用绑定在文本框旁边的标签显示错误消息。以下是我如何在中继器中设置绑定的gridview: <!-- Collapsible panel extender body --> <asp:Panel ID="pBody1" runat="server" CssClass="cpBody"> <asp:Label ID="lblBodyText1" runa

我尝试这样做,如果在gridview的某些行上标记了复选框,我将检查用户输入是否超过存储级别。如果超过,我将使用绑定在文本框旁边的标签显示错误消息。以下是我如何在中继器中设置绑定的gridview:

<!-- Collapsible panel extender body -->
<asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
    <asp:Label ID="lblBodyText1" runat="server" />
    <!-- Grid view to show products based on each category -->
    <asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="998px" CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False" DataKeyNames="id">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:CheckBox ID="cbCheckRow" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>                               
            <asp:BoundField DataField="name" HeaderText="Name" ItemStyle-Width="650px" />
            <asp:BoundField DataField="inventoryQuantity" HeaderText="Total Unit" />
            <asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:TextBox ID="tbQuantity" runat="server" Width="60" Text='<%# DataBinder.Eval(Container.DataItem, "unitQuantity") %>'/>
                    <asp:Label ID="lblCheckAmount" runat="server" Visible="true" ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>
</asp:Panel>
以下是单击按钮时的代码:

string quantity = "", prodID = "";
int packagesNeeded = 0, totalUnit = 0;
Dictionary<string, string> tempList = new Dictionary<string, string>();

//Get the total packages needed for this distribution
packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

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 and unit quantity from selected row index
                prodID = gv.DataKeys[gr.RowIndex].Value.ToString();

                var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
                if (tbQuantity != null)
                {
                    quantity = tbQuantity.Text;
                }

                //Add both objects into Dictionary
                tempList.Add(prodID, quantity);
            }
        }
    }
}
//Loop thru tempList. key as prodID, tempList.Keys as quantity
foreach (string key in tempList.Keys)
{
    //Get total unit of each products
    totalUnit = prodPackBLL.getTotalProductUnit(key);
    //Here check if exceed storage
    if (((Convert.ToInt32(quantity)) * packagesNeeded) > totalUnit)
    {
        foreach (RepeaterItem item in Repeater1.Items)
        {
            Panel pnl = item.FindControl("pBody1") as Panel;
            GridView gv = pnl.FindControl("gvProduct") as GridView;
            foreach (GridViewRow gr in gv.Rows)
            {
                Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
                lblCheckAmount.Text = "Insufficient amount";
            }
        }
    }
} 
所有的复选框和其他东西都工作得很好。只是当一条记录不足时,所有行都将显示错误消息,即使它们足够了。我试图做的是在行上显示错误消息,这些消息仅是不够的

提前谢谢,很抱歉我解释得不好。

在gridview中循环时,您需要检查键值是否与每行的DataKey值匹配。以下是需要更改的部分:

    //Loop thru tempList. key as prodID, tempList.Keys as quantity
    foreach (string key in tempList.Keys)
    {
        //Get total unit of each products
        totalUnit = prodPackBLL.getTotalProductUnit(key);
        //Here check if exceed storage
        if (((Convert.ToInt32(tempList[key])) * packagesNeeded) > totalUnit)
        {
            foreach (RepeaterItem item in Repeater1.Items)
            {
                Panel pnl = item.FindControl("pBody1") as Panel;
                GridView gv = pnl.FindControl("gvProduct") as GridView;
                foreach (GridViewRow gr in gv.Rows)
                {
                    // compare the key with the data key of the current row
                    if (key == gv.DataKeys[gr.RowIndex].Value.ToString())
                    {
                        // display the insufficient message
                        Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
                        lblCheckAmount.Text = "Insufficient amount";
                    }
                }
            }
        }
    }

现在,错误消息的范围缩小到显示在选中的行。而检查只针对列表中的最后一项。假设我得到了Prod1,2,6,7。它只检查最后的项目。刚刚编辑了我的答案,我想你仍然使用数量变量而不是tempList[key]哦,对不起,我的错。很抱歉在同样的问题上再次打扰您。现在可以用了,非常感谢