从网格视图问题中删除记录后刷新网格视图
本文关键字:网格 视图 刷新 记录 问题 删除 | 更新日期: 2023-09-27 18:36:36
嗨,我
有这个问题,在我从网格视图中删除某些记录后,尽管记录已从数据库中删除,但该记录仍然存在于网格视图中
只有在我刷新页面后,记录才会从网格视图中消失
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridpurchase.DataSource = s.getpurchase();
gridpurchase.DataBind();
}
}
删除代码
protected void gridpurchase_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
string commandName = e.CommandName.ToString().Trim();
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
string PurchaseID = row.Cells[6].Text;
GridViewRow gvRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
Int32 rowind = gvRow.RowIndex;
switch (commandName)
{
case "DeletePurchase":
if (s.DelPurchase(Convert.ToInt32(PurchaseID)))
{
string msg = "alert('Purchase Deleted Successfully');";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');window.location.href='" + Request.RawUrl + "';", true);
}
else
{
string msg = "Purchase Not Deleted";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');window.location.href='" + Request.RawUrl + "';", true);
}
break;
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
}
ASPX 代码
<asp:GridView ID="gridpurchase" ShowFooter="True" AutoGenerateColumns="False" OnRowCommand="gridpurchase_RowCommand" runat="server" DataKeyNames="Purchase_ID" OnRowDataBound="OnRowDataBound" CssClass="table table-bordered text-nowrap">
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton5" runat="server" CssClass="btn btn-primary btn-xs" CausesValidation="False" CommandName="EditPurchase" Text=""><i class="glyphicon glyphicon-pencil"></i></asp:LinkButton>
</ItemTemplate>
<ControlStyle CssClass="btn btn-primary" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton6" runat="server" CausesValidation="False" OnClientClick="return confirm('Are you sure you want to delete this record ?');" CssClass="btn btn-primary btn-xs" CommandName="DeletePurchase" Text="Delete"><i class="glyphicon glyphicon-trash"></i></asp:LinkButton>
</ItemTemplate>
<ControlStyle CssClass="btn btn-danger" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Print" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton20" runat="server" CausesValidation="False" CommandName="PrintInvoice" CssClass="btn btn-primary btn-xs" Text=""><i class="glyphicon glyphicon-print"></i></asp:LinkButton>
</ItemTemplate>
<ControlStyle CssClass="btn btn-default " />
</asp:TemplateField>
<asp:BoundField HeaderText="Purchase ID" DataField="Purchase_ID" InsertVisible="False" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" ReadOnly="True" SortExpression="Purchase_ID">
<asp:BoundField HeaderText="Purchase Date" HeaderText="Purchase Date">
</Columns>
<EmptyDataTemplate>
"No records found"
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#D1DDF1" ForeColor="#333333" />
</asp:GridView>
我认为您必须在删除数据库中的记录后调用 DataBind 来更新网格。