Skip to main content
 首页 » 编程设计

c#之使用 UpdatePanel 删除行后更新/刷新 Gridview。 ASP.NET C# AJAX

2024年01月02日32zhenyulu

我正在尝试更新 gridview,以便一旦从中删除了一行,它就会刷新并且不再自动显示该行。然而,这似乎不起作用。

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

    <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(); 

删除行成功

为什么这不起作用?

请您参考如下方法:

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

像这样:

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. 
 
} 

最好的问候