我正在尝试在gridView 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只包含已标记为已选中的所有行。我想知道为什么会这样。提前谢谢。
答案 0 :(得分:1)
执行tempList
时,distSPUItemList
和lbnConfirm_Click
变量将始终为空,因此您需要将它们存储在ViewState中,以便在回发之间保留其值。
试试这段代码:
private Dictionary<string, string> tempList
{
get
{
if (ViewState["tempList"] == null)
{
return new Dictionary<string,string>();
}
else
{
return (Dictionary<string, string>)ViewState["tempList"];
}
}
set
{
ViewState["tempList"] = value;
}
}
private Dictionary<string, string> distSPUItemList
{
get
{
if (ViewState["distSPUItemList"] == null)
{
return new Dictionary<string, string>();
}
else
{
return (Dictionary<string, string>)ViewState["distSPUItemList"];
}
}
set
{
ViewState["distSPUItemList"] = value;
}
}
protected void tbQuantity_TextChanged(object sender, EventArgs e)
{
tempList = new Dictionary<string, string>();
distSPUItemList = new Dictionary<string, string>();
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]);
}
}
}