0

如果 ASP.Net 按钮和 GridView 位于不同的 asp:Content 块中,我们希望刷新 ASP.Net GridView

在一个正常工作的 ASP.Net Web 表单中,我们有一个 DataSource、GridView、DetailsView、各种其他控件、一个 asp:TextBox 和一个 asp:Button,用于根据用户在文本框中输入的内容来搜索数据。所有这些都在一个 asp:Content 块中,它还有一个 asp:UpdatePanel。

我们决定更改表单的布局,将 GridView 和 DetailsView 分开,并将它们放入另一个 asp:Content 块中。运行表单时,所有内容都显示在屏幕上的正确位置,并且还按预期显示了数据库中的数据。

我们发现如果用户输入搜索条件并单击搜索按钮,代码隐藏文件中的代码确实执行了,但 GridView 没有刷新。

我将假设需要在代码隐藏文件中添加一些额外的编码来做到这一点。

这是来自 asp:Content 块之一的搜索按钮的标记:

<asp:Content 
ID="ContentBody" 
ContentPlaceHolderID="BodyPlaceholder" 
runat="server">

<% '-- Ajax enable this area so flicker us cut down to a minumum. -- %>
<% '---------------------------------------------------------------- %>
<asp:UpdatePanel 
    ID="UpdatePanelSummary" 
    runat="server" 
    UpdateMode="Conditional">

    <ContentTemplate> 

        <h1>Classes / Subjects Maintenance</h1>

        Class Search:
        <asp:TextBox 
            ID="TextBoxSearch" 
            runat="server" 
            Width="207px" 
            Text="ALL">
        </asp:TextBox>

        <asp:Button 
            ID="ButtonSearch" 
            runat="server" 
            Text="Search"
            OnClick="ButtonSearch_Click" />

        <asp:Button 
            ID="ButtonSearchAll" 
            runat="server" 
            Text="Show ALL Classes" 
            OnClick="ButtonSearchAll_Click"/>

        <br />

        <asp:Button 
            ID="ButtonAddNewClass" 
            runat="server" 
            Text="Add a New Class to this List" />
        <br />

        <strong><span class="auto-style1">
        <br />
        To send an email of this list, enter the email address of whom you wish to send it to then click the envelope.</span></strong>        
        <br />
        <br />

        Recipient:
        <asp:TextBox ID="TextBoxEmailRecipient" runat="server" Width="203px"></asp:TextBox>
        <strong><span class="auto-style1">&nbsp;</span></strong>

        <asp:ImageButton 
            ID="ImageButtonEmailThisList" 
            runat="server" 
            BorderStyle="None" 
            ImageUrl="~/Images/email1.png" 
            OnClick="ImageButtonEmailThisList_Click" 
            ToolTip="Email this List as a report." Height="50px" Width="50px" 
        />

        <br />
        <asp:Label ID="LabelEmailMessage" runat="server" style="font-weight: 700; color: black"></asp:Label>
        <br />

    </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

这是具有 GridView 的 asp:Content 块的标记:

<asp:Content
ID="DetailsBody" 
ContentPlaceHolderID="DetailsPlaceholder" 
runat="server">

<asp:UpdatePanel 
    ID="UpdatePanelDetails" 
    runat="server" 
    UpdateMode="Conditional">

    <ContentTemplate>


        <% '-- GridView (Grid) for summary.                                                -- %>
        <% '-- The user chooses a Class from here and details are shown in a DetailsView.  -- %>
        <% '--------------------------------------------------------------------------------- %>

        <asp:GridView
            ID="GridViewSummary" 
            runat="server" 
            AllowSorting="True" 
            AutoGenerateColumns="False" 
            DataKeyNames="ID" 
            Width="401px" 
            AllowPaging="True" 
            PageSize="3">

            <Columns>
                <asp:BoundField DataField="ClassName" HeaderText="Class / Subject" 
                    SortExpression="ClassName" >

                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>

                <asp:BoundField DataField="Grade" HeaderText="Grade" 
                    SortExpression="Grade" >

                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:BoundField>

                <asp:CommandField ButtonType="Button" SelectText="Select Class Details" 
                    ShowSelectButton="True"/>
            </Columns>
            <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NextPreviousFirstLast" NextPageText="Next" PreviousPageText="Previous"/>
        </asp:GridView>


    </ContentTemplate>
</asp:UpdatePanel>    

</asp:Content>

许多控件已被删除,因此此代码将更易于遵循。

这是用户单击搜索按钮后将数据加载到 GridView 的代码隐藏文件中的编码:

Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs)

    ' Show the schedules the user wants.
    '-----------------------------------
    GridViewSummary.DataSource = theTableAdapter.GetDataByAllClasses(TextBoxSearch.Text)
    GridViewSummary.DataBind()
End Sub
4

1 回答 1

1

您可以UpdatePanelDetails.Update()在绑定数据后调用ButtonSearch_Click

Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs)

    ' Show the schedules the user wants.
    '-----------------------------------
    GridViewSummary.DataSource = theTableAdapter.GetDataByAllClasses(TextBoxSearch.Text)
    GridViewSummary.DataBind()
    UpdatePanelDetails.Update()
End Sub
于 2013-05-13T18:29:25.397 回答