如何在将新行添加到 asp 网格时保留前一行值

本文关键字:保留 一行 网格 新行 添加 asp | 更新日期: 2023-09-27 18:31:34

我有一个动态绑定数据的asp网格。在同一个网格中,我添加了带有文本框的新行。问题是添加另一个新行不会保留前一行数据。

这是网格结构和代码。

ASPX代码在这里:

<asp:GridView ID="gvReceivedComponent" runat="server" ShowFooter="true" AutoGenerateColumns="false" CssClass="list">
    <HeaderStyle CssClass="searchResultHeader" />
    <RowStyle CssClass="searchResultRow" />
    <AlternatingRowStyle CssClass="searchResultAltRow" />
    <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" Visible="false" />
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="chkItemRetComp" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Component #">
            <ItemTemplate>
                <asp:Label ID="lblgvModel" runat="server" Text='<%# Eval("MODELNO") %>'></asp:Label>
                <asp:TextBox runat="server" ID="txtModel" Width="85px" MaxLength="20" onkeypress="uppercase();" />
                <asp:Button runat="server" ID="btnShowModel" CssClass="searchbutton" Width="20px" Text="..." ToolTip="Search for Model" />
                <input type="hidden" id="hdnModelDesc" runat="server" value="" />
                <input type="hidden" id="hdnModelIdy" runat="server" value="0" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Component Description">
            <ItemTemplate>
                <asp:Label ID="lblgvModelDesc" runat="server" Text='<%# Eval("MODELDESC") %>'></asp:Label>
                <asp:TextBox ID="txtModelDesc" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText=" BOM Qty">
            <ItemTemplate>
                <span style="width: 20px">
                    <asp:Label ID="lblgvBOMRecvCompQty" runat="server" Text='<%# Eval("QTY") %>'></asp:Label></span>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Actual Qty" HeaderStyle-Width="90px">
            <ItemTemplate>
                <asp:TextBox ID="txtActRecvCompQty" Width="30px" runat="server" Text='<%# Eval("QTY") %>' onkeypress="return IsNumeric()" onKeyDown="javascript:return ResetRCompChkBox(event);"></asp:TextBox>
                <asp:HiddenField ID="hdnFlagtohidegvControls" runat="server" Value='<%#Eval("ISNEW")%>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C#代码在这里:

void gvReceivedComponent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btnShowModel = (Button)e.Row.FindControl("btnShowModel");
            TextBox txtModel = (TextBox)e.Row.FindControl("txtModel");
            TextBox txtModelDesc = (TextBox)e.Row.FindControl("txtModelDesc");
            HtmlInputHidden hdnModelDesc = (HtmlInputHidden)e.Row.FindControl("hdnModelDesc");
            HtmlInputHidden hdnModelIdy = (HtmlInputHidden)e.Row.FindControl("hdnModelIdy");
            HiddenField hdnFlagtohidegvControls = (HiddenField)e.Row.FindControl("hdnFlagtohidegvControls");
            btnShowModel.Attributes.Add("onclick", "return ShowDivPopup('" + Constants.LookupCategory.CLIENTCUSTOMERMODEL + "', '" + Constants.LookupCategorySearchParms.CLIENTCUSTOMERMODELSEARCH + "', '" + txtModel.ClientID + "', '" + txtModelDesc.ClientID + "', 'false', '" + frmRecvRepairs.ID + "', '3','" + hdnModelIdy.ClientID + "');");
            if (hdnFlagtohidegvControls.Value == "Y")
            {
                txtModel.Visible = false;
                txtModelDesc.Visible = false;
                btnShowModel.Visible = false;
            }
            else
            {
                txtModel.Visible = true;
                txtModelDesc.Visible = true;
                btnShowModel.Visible = true;
            }
        }
    }

绑定网格

if (dsRAComponent != null)
        {
            if (dsRAComponent.Tables.Count > 0)
            {
                dsRAComponent.Tables[0].Columns.Add(new DataColumn("ISNEW"));
                foreach (DataRow dr in dsRAComponent.Tables[0].Rows)
                {
                    dr["ISNEW"] = "Y";
                }
                if (dsRAComponent.Tables[0].Rows.Count > 0)
                {
                    gvReceivedComponent.DataSource = dsRAComponent;
                    gvReceivedComponent.DataBind();
                }
                Session["dsRAComponent"] = dsRAComponent.Tables[0];
            }

添加新行

 if (Session["dsRAComponent"] != null)
        {
            DataTable dtCurrentTable = (DataTable)Session["dsRAComponent"];
            DataRow drCurrentRow;
            drCurrentRow = dtCurrentTable.NewRow();
            drCurrentRow["MODELNO"] = String.Empty;
            drCurrentRow["MODELDESC"] = String.Empty;
            drCurrentRow["QTY"] = 0;
            dtCurrentTable.Rows.InsertAt(drCurrentRow, 0);
            Session["dsRAComponent"] = dtCurrentTable;
            //Rebind the Grid with the current data
            gvReceivedComponent.DataSource = dtCurrentTable;
            gvReceivedComponent.DataBind();
        }

如何在将新行添加到 asp 网格时保留前一行值

在 pagelod 事件中,您是否通过检查来绑定数据!是回发 ?尝试每次都绑定它。希望这有帮助。

可以使用 ViewState 属性来保留以前的数据。检查此链接