2013-05-13 75 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