【发布时间】: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