【问题标题】:Display error message on gridview row based on textBox value根据 textBox 值在 gridview 行上显示错误消息
【发布时间】:2013-12-29 16:15:11
【问题描述】:

如果在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";
            }
        }
    }
} 

所有复选框和其他东西都可以正常工作。只是当一条记录不足时,即使足够了,所有行都会显示错误消息。我想要做的是仅在不足的行上显示错误消息。

在此先感谢,并为我糟糕的解释感到抱歉。

【问题讨论】:

    标签: c# asp.net gridview dictionary textbox


    【解决方案1】:

    在遍历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";
                        }
                    }
                }
            }
        }
    

    【讨论】:

    • 现在错误消息缩小显示在选中的行。并且检查仅针对列表中的最后一项。假设我得到了产品 1,2,6,7。它只检查最后一项。
    • 刚刚编辑了我的答案,我想你仍然使用数量变量而不是 tempList[key]
    • 哦,对不起,我的错。很抱歉在同样的问题上再次给您带来困扰。它现在可以工作了,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多