GridView无法在updatepanel内部更新?

时间:2019-02-15 14:47:48

标签: c# asp.net

我的gridview在一个updatepanel中,由于某种原因,Gridview没有显示更新的数据。我想念什么吗?我的代码看起来像我发现的其他示例。

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" ColumnName="test" CssClass="auto-style14" EnableViewState="false" Font-Size="Medium" HorizontalAlign="Left" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="1010px">
                    <Columns>
                        <asp:ButtonField Text="Details" />
                    </Columns>
                    <FooterStyle BackColor="White" ForeColor="#000066" />
                    <HeaderStyle BackColor="#006699" BorderStyle="Solid" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                    <RowStyle ForeColor="#000066" />
                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#007DBB" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#00547E" />
                    <RowStyle HorizontalAlign="Center" />
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>

CS:

        }
        GridView1.DataSource = visualDataTable;
        GridView1.DataBind();
        UpdatePanel1.Update();
    }

1 个答案:

答案 0 :(得分:0)

首先,您需要为GridView设置AutoPostBack="true",并且应在UpdatePanel中提供触发器,以根据特定事件更新GridView,如以下示例所示:

<Triggers>
     <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanged" />
</Triggers>

无论何时发生此事件,都会更新您的GridView。或者,如果您想在单击按钮时更新GridView,则需要在触发器内提供以下代码行:

<asp:AsyncPostBackTrigger ControlID="btnUpdate" EventName="Click" />

这是您更新的GridView:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
  <ContentTemplate>
    <asp:GridView
      ID="GridView1"
      runat="server"
      AutoGenerateSelectButton="True"
      BackColor="White"
      BorderColor="#CCCCCC"
      BorderStyle="None"
      BorderWidth="1px"
      CellPadding="3"
      ColumnName="test"
      CssClass="auto-style14"
      EnableViewState="false"
      Font-Size="Medium"
      HorizontalAlign="Left"
      AutoPostBack="true"
      OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
      Width="1010px"
    >
      <Columns>
        <asp:ButtonField Text="Details" />
      </Columns>
      <FooterStyle BackColor="White" ForeColor="#000066" />
      <HeaderStyle
        BackColor="#006699"
        BorderStyle="Solid"
        Font-Bold="True"
        ForeColor="White"
        HorizontalAlign="Center"
        VerticalAlign="Middle"
      />
      <PagerStyle
        BackColor="White"
        ForeColor="#000066"
        HorizontalAlign="Left"
      />
      <RowStyle ForeColor="#000066" />
      <SelectedRowStyle
        BackColor="#669999"
        Font-Bold="True"
        ForeColor="White"
      />
      <SortedAscendingCellStyle BackColor="#F1F1F1" />
      <SortedAscendingHeaderStyle BackColor="#007DBB" />
      <SortedDescendingCellStyle BackColor="#CAC9C9" />
      <SortedDescendingHeaderStyle BackColor="#00547E" />
      <RowStyle HorizontalAlign="Center" />
    </asp:GridView>
  </ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger
      ControlID="GridView1"
      EventName="SelectedIndexChanged" />
  </Triggers>
</asp:UpdatePanel>