【发布时间】:2013-12-30 01:24:24
【问题描述】:
我尝试在gridView中的textbox实现onTextChanged时,它会autoPostBack检查存储级别是否足够。我使用中继器和可折叠面板扩展器设置了我的 gridView。如果足够,它将存储到一个数组中。然后,当单击按钮时,它将遍历数组并执行插入 Sql 语句。以下是我设置 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") %>' OnTextChanged="tbQuantity_TextChanged" AutoPostBack="true"/>
<asp:Label ID="lblCheckAmount" runat="server" ForeColor="Red" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
textBox OnTextChanged 方法时,它会从选定的行中获取 prodID 和 textBox 值,然后循环执行检查。如果不够,我用标签显示错误信息:
//Dictionary used to pair up prodID and quantity
Dictionary<string, string> tempList = new Dictionary<string, string>();
Dictionary<string, string> distSPUItemList = new Dictionary<string, string>();
protected void tbQuantity_TextChanged(object sender, EventArgs e)
{
string quantity = "", prodID = "";
int packagesNeeded = 0, totalUnit = 0;
//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)
{
//Clear label error message
Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
lblCheckAmount.Text = "";
//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);
//lblTest.Text += key + " " + tempList[key] + "units";
//Check if unitQuantity exceed storage level
if (((Convert.ToInt32(tempList[key])) * packagesNeeded) > totalUnit)
{
//Get the label control in gridview
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";
}
}
}
}
else
{
distSPUItemList.Add(key, tempList[key]);
}
}
}
当我点击按钮时,我得到了经过验证的列表,它是 distSPUItemList 但不是 tempList:
protected void lbnConfirm_Click(object sender, EventArgs e)
{
//Loop thru distSPUItemList. key as prodID, distSPUItemList.Keys as quantity
foreach (string key in distSPUItemList.Keys)
{
lblTest.Text += key + " " + distSPUItemList[key] + "units";
}
}
但是,在我的 lblTest 中,列表中没有存储任何内容。我不想将经过验证的存储在 tempList 中,因为 tempList 只包含所有标记为已检查的行。我想知道为什么会这样。提前致谢。
【问题讨论】:
标签: c# asp.net gridview dictionary textbox