【问题标题】:Updating/Refreshing a Gridview once a row has been deleted Using UpdatePanel. ASP.NET C# AJAX使用 UpdatePanel 删除行后更新/刷新 Gridview。 ASP.NET C# AJAX
【发布时间】:2012-03-21 20:36:36
【问题描述】:

我正在尝试更新网格视图,以便一旦从中删除一行,它将刷新并且不再自动显示该行。但是,这似乎不起作用。

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

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

一旦成功删除该行。

为什么这不起作用?

【问题讨论】:

  • 您是从 GridView 控件、数据绑定到 GridView 或从其他位置(如数据库)中删除该行吗?
  • 是的,它们存储在数据库中,gridview 显示这些结果。因此,当我删除某些内容时,我会将其从数据库中删除,并希望 Gridview 更新以显示数据库结果。
  • 在调用UpdatePanel1.Update()之前是否要重新绑定网格?
  • 处理删除后使用 Gridview.binddata()..

标签: c# asp.net ajax gridview updatepanel


【解决方案1】:

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

类似这样的:

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.

}

最好的问候

【讨论】:

    【解决方案2】:

    删除行后需要再次绑定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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      相关资源
      最近更新 更多