使用UpdatePanel在删除行后更新/刷新网格视图.ASP.NET C#AJAX

本文关键字:视图 网格 ASP NET C#AJAX 刷新 UpdatePanel 删除行 更新 使用 | 更新日期: 2023-09-27 18:28:39

我正在尝试更新网格视图,这样一旦从中删除了一行,它就会刷新,不再自动显示该行。然而,这似乎并不奏效。

到目前为止,我已经尝试过:

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional" >
    <ContentTemplate>
        <asp:GridView ID="BookingsGridView" runat="server" AutoGenerateColumns="False" 
            BorderColor="ForestGreen" BorderStyle="Ridge" BorderWidth="10px" 
            CellPadding="4" ForeColor="#333333" GridLines="None" 
            onrowdatabound="dgTest_RowDataBound" OnSelectedIndexChanged="Cancel_Booking">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowSelectButton="true" SelectImageUrl="~/Images/Icons/Cross.png" SelectText="Cancel"/>
                <asp:BoundField DataField="Book_id" HeaderText="Book_id"/>
                <asp:BoundField DataField="Username" HeaderText="Username" />
                <asp:BoundField DataField="Client" HeaderText="Client" />
                <asp:BoundField DataField="Description" HeaderText="Description" />
                <asp:BoundField DataField="BookingDate" HeaderText="BookingDate" />
                <asp:BoundField DataField="Duration" HeaderText="Duration" />
                <asp:BoundField DataField="Location" HeaderText="Location" />
                <asp:BoundField DataField="Payment" HeaderText="Payment" />
            </Columns>
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#546E96" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333"/>
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
            <SelectedRowStyle CssClass="SelectedRowStyle" />
        </asp:GridView>
        <br />
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="BookingsGridView" />
    </Triggers>
</asp:UpdatePanel>

然后我打电话给

 UpdatePanel1.Update();

一旦成功删除该行。

为什么不起作用?

使用UpdatePanel在删除行后更新/刷新网格视图.ASP.NET C#AJAX

如果您有将数据从数据库绑定到datagridview的Read方法,只需在delete方法上再次绑定即可。

像这样的东西:

private void Read()
{
  ///binding stuff datagridview.datasource = datasource;
}

private void Delete()
{
//Your delete stuff
//call
Read(); // this will refresh your grid after deleting a record.
}

向致以最诚挚的问候

删除行后需要再次绑定GridView。像这样的东西。

public void GetData_BookingsGridView
{
   .......//your code
   BookingsGridView.DataSource=dt;
   BookingsGridView.DataBind();
}
public void DeleteBookingGridView_Row()
{ 
  //code for deleting gridview rows.
  //now call GetData_BookingsGridView function to refresh GridView
  GetData_BookingsGridView();
}