基于textBox值在gridview行上显示错误消息

时间:2013-12-29 16:15:11

标签: c# asp.net gridview dictionary textbox

我试着这样做,如果在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";
            }
        }
    }
} 

所有复选框和其他内容完美无缺。只是当一条记录不足时,所有行都会显示错误消息,即使它们已足够。我想要做的是在行上显示仅仅不足的错误消息。

提前致谢并对我糟糕的解释感到抱歉。

1 个答案:

答案 0 :(得分:0)

在循环遍历gridview时,您需要检查key值是否与每行的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";
                    }
                }
            }
        }
    }