【问题标题】:TextBox OnTextChanged in Gridview does not store value into listGridview 中的 TextBox OnTextChanged 不会将值存储到列表中
【发布时间】: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


    【解决方案1】:

    tempListdistSPUItemList 变量在执行 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]);
            }
        }
    }
    

    【讨论】:

    • 我认为您单击按钮而不更改任何文本框值,因此执行 lbnConfirm_Click 时 tempList 和 distSPUItemList 仍然为空。你能确认是不是这样吗?
    • 哦,对不起我的错误。所以基本上字典列表在 isPostBack 时会变空,所以我们需要一个 viewState 来持久化里面的项目?
    • 是的,如果您想在不更改文本框值的情况下保留项目,那么您需要从 lbnConfirm_Click 中的 tbQuantity_TextChanged 执行相同的循环
    • 好的,请注意。非常感谢。但只是想问一下,你知道 LINQ 查询吗?因为我在使用 LINQ 查询检索某些内容时遇到问题
    • 是的,我愿意,你可以发布一个新问题,我会尝试回答,或者如果我不能回答,其他人会。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2010-11-18
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多