C# gridview中的文本框不自动回发
MyC# gridview中的文本框不自动回发,c#,asp.net,gridview,textbox,C#,Asp.net,Gridview,Textbox,Mygridview回发时缺少以前的数据。下面是一个场景,当我从gvFinalizedgridview中检测到库存不足时,我会得到类别,然后我会得到库存最高的产品,并显示在gvSuggeredgridview中 但假设在我的gvFinalized中,有三种不同类别的产品是不够的,比如面条、罐头食品和饮料。它应该展示三种不同的产品,每种产品都来自gv中的不同类别 然而,我现在的问题是,它只显示最后的项目。例如,在此场景中,饮料。饮料前的数据被抹掉了 以下是我如何检测库存不足的代码: protect
gridview
回发时缺少以前的数据。下面是一个场景,当我从gvFinalized
gridview中检测到库存不足时,我会得到类别,然后我会得到库存最高的产品,并显示在gvSuggered
gridview中
但假设在我的gvFinalized
中,有三种不同类别的产品是不够的,比如面条、罐头食品和饮料。它应该展示三种不同的产品,每种产品都来自gv
中的不同类别
然而,我现在的问题是,它只显示最后的项目。例如,在此场景中,饮料。饮料前的数据被抹掉了
以下是我如何检测库存不足的代码:
protected void tbQuantity_TextChanged(object sender, EventArgs e)
{
tempList = new Dictionary<string, string>();
distSPUItemList = new Dictionary<string, int>();
bool valid = true;
string quantityStr = "", prodID = "";
int packagesNeeded = 0, totalUnit = 0, quantity = 0;
//Get the total packages needed for this distribution
packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
foreach (GridViewRow gr in gvFinalised.Rows)
{
//Clear label error message
Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
lblCheckAmount.Text = "";
//Get the product variant ID which set as DataKeyNames and product quantity from selected row index
prodID = gvFinalised.DataKeys[gr.RowIndex].Value.ToString();
var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
if (tbQuantity != null)
{
//Check if the input is numeric
quantityStr = tbQuantity.Text;
if (!int.TryParse(quantityStr, out quantity))
{
lblCheckAmount.Text = "Non-numeric input!";
TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
}
else
{
//Add both objects into Dictionary
tempList.Add(prodID, quantityStr);
}
}
}
//Portion to check the storage level for each products stored in tempList
//Loop thru tempList. key as prod variant ID, tempList.Keys as quantity
foreach (string key in tempList.Keys)
{
//Get total unit of each products
totalUnit = prodPackBLL.getTotalProductUnit(key);
valid = true;
//Check if unitQuantity exceed storage level
if (((Convert.ToInt32(tempList[key])) * packagesNeeded) > totalUnit)
{
//Get the label control in gridview
foreach (GridViewRow gr in gvFinalised.Rows)
{
if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
{
//Change the color of textBox and display the insufficient message
valid = false;
//Automatically uncheck the checkBox if invalid
TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
lblCheckAmount.Text = "Insufficient stock!";
//Here is the place where I collect the data and display in gvSuggested
getSuggested(key);
}
}
}
else
{
if (totalUnit - ((Convert.ToInt32(tempList[key])) * packagesNeeded) == 0)
{
foreach (GridViewRow gr in gvFinalised.Rows)
{
if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
{
valid = true;
Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
lblCheckAmount.Attributes["style"] = "color:#ffb848";
lblCheckAmount.Text = "Stock becomes 0!";
}
}
}
}
//Portion to check for valid products to be inserted into distSPUItemList
if (valid)
{
//Set the textBox color
foreach (GridViewRow gr in gvFinalised.Rows)
{
if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
{
TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
}
}
//Validated items store into another list to perform Sql statement when button on click
distSPUItemList.Add(key, (Convert.ToInt32(tempList[key]) * packagesNeeded));
}
}
}
this.SuggestedItems = distSPUItem;
回发时,我使用viewState
存储数据。但是,它不起作用。中的这一行建议:
protected void tbQuantity_TextChanged(object sender, EventArgs e)
{
tempList = new Dictionary<string, string>();
distSPUItemList = new Dictionary<string, int>();
bool valid = true;
string quantityStr = "", prodID = "";
int packagesNeeded = 0, totalUnit = 0, quantity = 0;
//Get the total packages needed for this distribution
packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
foreach (GridViewRow gr in gvFinalised.Rows)
{
//Clear label error message
Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
lblCheckAmount.Text = "";
//Get the product variant ID which set as DataKeyNames and product quantity from selected row index
prodID = gvFinalised.DataKeys[gr.RowIndex].Value.ToString();
var tbQuantity = gr.FindControl("tbQuantity") as TextBox;
if (tbQuantity != null)
{
//Check if the input is numeric
quantityStr = tbQuantity.Text;
if (!int.TryParse(quantityStr, out quantity))
{
lblCheckAmount.Text = "Non-numeric input!";
TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
}
else
{
//Add both objects into Dictionary
tempList.Add(prodID, quantityStr);
}
}
}
//Portion to check the storage level for each products stored in tempList
//Loop thru tempList. key as prod variant ID, tempList.Keys as quantity
foreach (string key in tempList.Keys)
{
//Get total unit of each products
totalUnit = prodPackBLL.getTotalProductUnit(key);
valid = true;
//Check if unitQuantity exceed storage level
if (((Convert.ToInt32(tempList[key])) * packagesNeeded) > totalUnit)
{
//Get the label control in gridview
foreach (GridViewRow gr in gvFinalised.Rows)
{
if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
{
//Change the color of textBox and display the insufficient message
valid = false;
//Automatically uncheck the checkBox if invalid
TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
lblCheckAmount.Text = "Insufficient stock!";
//Here is the place where I collect the data and display in gvSuggested
getSuggested(key);
}
}
}
else
{
if (totalUnit - ((Convert.ToInt32(tempList[key])) * packagesNeeded) == 0)
{
foreach (GridViewRow gr in gvFinalised.Rows)
{
if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
{
valid = true;
Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
lblCheckAmount.Attributes["style"] = "color:#ffb848";
lblCheckAmount.Text = "Stock becomes 0!";
}
}
}
}
//Portion to check for valid products to be inserted into distSPUItemList
if (valid)
{
//Set the textBox color
foreach (GridViewRow gr in gvFinalised.Rows)
{
if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
{
TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
}
}
//Validated items store into another list to perform Sql statement when button on click
distSPUItemList.Add(key, (Convert.ToInt32(tempList[key]) * packagesNeeded));
}
}
}
this.SuggestedItems = distSPUItem;
导致gridView中的文本框无法自动回发。如果我删除了它,上面的错误会再次出现。有导游吗?提前感谢。请启用tbQuantity文本框EnableAutoPostBack=True有人知道原因吗?这是因为viewState问题还是我的逻辑问题?您是否将
[Serializable]
属性添加到分发标准打包单元项
类中?@afzalugh它可以工作。但为什么我需要在类前面添加[Serializable]?在ViewState中保存列表时,需要序列化它们。看见我想知道为什么你没有得到任何错误!我以前有过这样的经历,当我忘记将我的类标记为可序列化时,列表中只保存了一个元素!我相信你的问题值得投票表决!由于情况复杂,这个问题没有得到足够的重视。