我有一个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#CODE HERE:
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();
}
答案 0 :(得分:0)
在pagelod事件中,你通过检查绑定数据!IsPostBack? 尝试每次绑定它。 希望这会有所帮助。
答案 1 :(得分:0)
您可以使用ViewState属性保留以前的数据。 请检查此link