繁体   English   中英

Gridview中的TextBox OnTextChanged不会将值存储到列表中

[英]TextBox OnTextChanged in Gridview does not store value into list

我正在尝试实现gridView onTextChanged中的文本框时,它将自动过帐以检查存储级别是否足够。 我使用中继器和可折叠面板扩展器设置了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仅包含所有标记为选中的行。 我不知道为什么会这样。 提前致谢。

当执行lbnConfirm_Click时, tempListdistSPUItemList变量将始终为空,因此您需要将它们存储在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]);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM