如何在添加新行后保持网格视图中的行值
本文关键字:视图 网格 添加 新行 | 更新日期: 2023-09-27 18:25:53
我有一个网格视图,其中包含项目编号、描述数量、成本、extncost的文本框。当在文本框中输入itemno时,desc和项目成本将在文本框事件更改时自动出现。在这里,当我在网格视图中添加一个新行时,上次输入的值got的值就会消失。当我使用断点进行检查时,我可以看到数据表中最后输入的值。由于,添加新行时有空白文本框,现在系统将空白文本框视为文本更改事件。因此,上次输入的值也不会显示。一行中的文本框更改也会影响其他行。
这是ASPX.页面代码:
<asp:UpdatePanel ID="gin_pnlupdt" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:GridView ID="grv_gindet" runat="server" ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"
OnRowDataBound="grv_gindtrowcmd" OnRowCommand="grv_gindetrowcmd">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="SNo" />
<asp:TemplateField HeaderText="Item Number">
<ItemTemplate>
<asp:TextBox ID="txt_itemno" runat="server" OnTextChanged="txt_itemno_changed" AutoPostBack="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Description">
<ItemTemplate>
<asp:Label ID="txt_itemdesc" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txt_qty" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Cost">
<ItemTemplate>
<asp:Label ID="txt_ucost" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Extended Cost">
<ItemTemplate>
<asp:Label ID="txt_extncost" runat="server"></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="BtnAddRow" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="grv_gindet" />
</Triggers>
</asp:UpdatePanel>
这是添加新行的CS代码:
protected void AddNewRow()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox TextBoxItem =
(TextBox)grv_gindet.Rows[rowIndex].Cells[1].FindControl("txt_itemno");
Label TextBoxDesc =
(Label)grv_gindet.Rows[rowIndex].Cells[2].FindControl("txt_itemdesc");
TextBox TextBoxQty =
(TextBox)grv_gindet.Rows[rowIndex].Cells[3].FindControl("txt_qty");
Label TextBoxucost =
(Label)grv_gindet.Rows[rowIndex].Cells[4].FindControl("txt_ucost");
Label TextBoxextncost =
(Label)grv_gindet.Rows[rowIndex].Cells[5].FindControl("txt_extncost");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxItem.Text;
dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxDesc.Text;
dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxQty.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = TextBoxucost.Text;
dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxextncost.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grv_gindet.DataSource = dtCurrentTable;
grv_gindet.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
检索以前数据的代码:
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox TextBoxItem = (TextBox)grv_gindet.Rows[rowIndex].Cells[1].FindControl("txt_itemno");
Label TextBoxDesc = (Label)grv_gindet.Rows[rowIndex].Cells[2].FindControl("txt_itemdesc");
TextBox TextBoxQty =
(TextBox)grv_gindet.Rows[rowIndex].Cells[3].FindControl("txt_qty");
Label TextBoxucost =
(Label)grv_gindet.Rows[rowIndex].Cells[4].FindControl("txt_ucost");
Label TextBoxextncost =
(Label)grv_gindet.Rows[rowIndex].Cells[5].FindControl("txt_extncost");
TextBoxItem.Text = dt.Rows[i]["Col1"].ToString();
TextBoxDesc.Text = dt.Rows[i]["Col2"].ToString();
TextBoxQty.Text = dt.Rows[i]["Col3"].ToString();
TextBoxucost.Text = dt.Rows[i]["Col4"].ToString();
TextBoxextncost.Text = dt.Rows[i]["Col5"].ToString();
rowIndex++;
}
}
}
}
文本框更改事件:
protected void txt_itemno_changed(object sender, EventArgs e)
{
//TextBox thisTextBox = (TextBox)sender;
//GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent;
//int row = thisGridViewRow.RowIndex;
GridViewRow currentrow = (GridViewRow)((TextBox)sender).Parent.Parent;
TextBox thisTextBox = (TextBox)currentrow.FindControl("txt_itemno");
int row = currentrow.RowIndex;
//rowChanged[row] = true;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AWCC"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ITEMDET.ITEMDESC,RGITEMDET.UNITCOST FROM ITEMDET JOIN RGITEMDET ON RGITEMDET.ITEMNO=ITEMDET.ITEMNO WHERE ITEMDET.ITEMNO ='" + thisTextBox.Text + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
grv_gindet.Rows[row].Cells[2].Text = dr["ITEMDESC"].ToString();
grv_gindet.Rows[row].Cells[4].Text = dr["UNITCOST"].ToString();
}
}
thisTextBox.Enabled = false;
}
gridview子元素的Postback控制代码:
protected void grv_gindtrowcmd(object sender, GridViewRowEventArgs e)
{
try
{
TextBox txtitm = e.Row.FindControl("txt_itemno") as TextBox;
LinkButton lnkbtn = e.Row.FindControl("ShowDeleteButton") as LinkButton;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtitm);
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lnkbtn);
}
catch
{
}
}
protected void grv_gindetrowcmd(object sender, GridViewCommandEventArgs e)
{
try
{
Button btnad = grv_gindet.FooterRow.FindControl("ButtonAdd") as Button;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btnad);
}
catch
{
}
}
Kinldy尽快提供解决方案,做必要的事情。
我希望这能帮助你,在Textchange事件中更改它
GridViewRow currentrow = (GridViewRow)((TextBox)sender).Parent.Parent.Parent.Parent;
TextBox thisTextBox = (TextBox)currentrow.FindControl("txt_itemno");
if (!string.IsNullOrWhiteSpace(thisTextBox.Text))
{
int row = currentrow.RowIndex;
//rowChanged[row] = true;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AWCC"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT ITEMDET.ITEMDESC,RGITEMDET.UNITCOST FROM ITEMDET JOIN RGITEMDET ON RGITEMDET.ITEMNO=ITEMDET.ITEMNO WHERE ITEMDET.ITEMNO ='" + thisTextBox.Text + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
((Label)(grv_gindet.Rows[row].FindControl("txt_itemdesc"))).Text = dr["ITEMDESC"].ToString();
((TextBox)(grv_gindet.Rows[row].FindControl("txt_ucost"))).Text = dr["UNITCOST"].ToString();
}
con.Close();
}
thisTextBox.Enabled = false;
}