2013-05-13 74 views
0

如果ASP.Net按鈕和GridView在不同的asp:Content中,我們想刷新ASP.Net GridView塊刷新ASP.Net GridView如果ASP.Net按鈕和GridView位於不同的asp:內容塊

在工作的ASP.Net Web窗體中,我們有一個DataSource,GridView,DetailsView,各種其他控件,一個asp:TextBox和一個asp:Button用於根據用戶輸入文本框來搜索數據。所有這些都在一個單獨的asp:Content塊中,並且它也有一個asp:UpdatePanel。

我們決定更改表單的佈局,並將GridView和DetailsView分開,並將它們放到另一個asp:Content塊中。當表單運行時,所有內容都顯示在屏幕上的正確位置,並顯示數據庫中的數據。

我們發現,如果用戶輸入搜索條件並單擊搜索按鈕,代碼隱藏文件中的代碼執行了,但GridView沒有刷新。

我打算假設需要在代碼隱藏文件中添加一些額外的編碼來完成此操作。

這裏是從ASP的一個搜索按鈕的標記:內容塊:

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

這是ASP的標記:具有在GridView內容塊:

<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 

回答

1

你可以調用bind數據後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 
+0

完善!編碼工作。 :-) – 2013-05-13 18:33:29