更新面板刷新ASP.Net gridview

时间:2015-08-10 09:14:06

标签: asp.net gridview

我在更新面板中有一个ASP.Net网格视图。我在Modalpopup扩展器中显示数据。我也在使用实体框架。 以下代码有效,但我需要确保使用最佳做法

ASP.Net Gridview

 <asp:ScriptManager ID="ScriptManager1" runat="server">   </asp:ScriptManager>
 <asp:UpdatePanel ID="UpdatePanel1"  UpdateMode="Conditional" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="grdOutput" />
        <asp:AsyncPostBackTrigger ControlID="btnDelete"  />
        <asp:AsyncPostBackTrigger ControlID="btnSave"  />
    </Triggers>
    <ContentTemplate>
        <asp:Button ID="btnNew" OnClick="btnNew_Click" Text=" new"  runat="server"/>
            <asp:GridView ID="grdOutput" runat="server" Width="200px" AutoGenerateColumns="False"  DataKeyNames="Id" >
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:TemplateField HeaderText="Edit">
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkEdit" runat="server" OnClick="lnkEdit_Click">Edit</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
         <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=FMPEntities" DefaultContainerName="FMPCommsEngEntities" EnableFlattening="False" EntitySetName="OutputTypes" EntityTypeFilter="OutputType">
         </asp:EntityDataSource>
         <asp:Panel ID="pnlEdit" runat="server" CssClass="modalPopup" style = "display:none">
     <table >
        <tr><td colspan="2"> Output Type Details</td></tr>
        <tr>
            <td>
                <asp:Label ID="lblOutputTypeID" runat="server" Text="ID"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtOutPutTypeID" runat="server"></asp:TextBox>
            </td>
       </tr>
       <tr>
            <td>
                <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtOutputTypeName" runat="server"></asp:TextBox>
            </td>

       </tr>
       <tr>
           <td> <asp:Button ID="btnSave" runat="server" OnClick="Save" Text="Save" /></td>
           <td><asp:Button ID="btnCancel" runat="server" Text="Cancel"  /></td>
           <td><asp:Button Id="btnDelete" runat="server" Text="Delete" OnClick="Delete" /></td>
       </tr>
    </table>
</asp:Panel> 
<asp:Panel ID="pnlAdd" runat="server" CssClass="modalPopup" style = "display:none">
    <table >
        <tr><td colspan="2"> Output Type Details</td></tr>
        <tr>
            <td>
                <asp:Label ID="lblADDName" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtAddName" runat="server"></asp:TextBox>
            </td>
        </tr>
       <tr>
           <td> <asp:Button ID="btnADDSave" runat="server" OnClick="Insert" Text="Save" /></td>
           <td><asp:Button ID="btnCancel1" runat="server" Text="Cancel"  /></td>
       </tr>
    </table>
</asp:Panel>
<cc1:ModalPopupExtender ID="popup" runat="server" CancelControlID="btnCancel" DropShadow="false"                                     PopupControlID="pnlEdit" TargetControlID = "link" BackgroundCssClass="modalBackground">
</cc1:ModalPopupExtender>
<cc1:ModalPopupExtender ID="AddPopup" runat="server" CancelControlID="btnCancel1" DropShadow="true" PopupControlID="pnlAdd" TargetControlID = "Addlink" BackgroundCssClass="modalBackground" >     </cc1:ModalPopupExtender> 
</ContentTemplate>
</asp:UpdatePanel>

我需要在插入,更新和删除后刷新gridview。

以下是我使用的代码。有没有更好的方法来刷新数据源。 我将gridview数据源设置为entitydatasource,并在每次保存,插入和删除之后对其进行数据绑定。

Protected Sub Save(sender As Object, e As EventArgs)
    UpdateOutputType(Integer.Parse(txtOutPutTypeID.Text), txtOutputTypeName.Text)
    grdOutput.DataSource = EntityDataSource1
    grdOutput.DataBind()

End Sub
Protected Sub Delete(sender As Object, e As EventArgs)
    DeleteOutputType(Integer.Parse(txtOutPutTypeID.Text))
    grdOutput.DataSource = EntityDataSource1
    grdOutput.DataBind()
End Sub

我尝试使用UpdatePanel1.update(),但gridview没有刷新。

刷新gridview的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

UpdatePanel1.update()不会更新您的网格。保存/删除方法中的方式是正确的。你需要重新绑定它。由于您对更新面板触发器进行了编码,因此更新面板将自动刷新。