【问题标题】:Trigger to refresh DataView when user clicks a button inside an UpdatePanel当用户单击 UpdatePanel 内的按钮时触发刷新 DataView
【发布时间】:2012-09-23 14:47:16
【问题描述】:

我们在 UpdatePanel 中有以下代码。

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

    <ContentTemplate> 
        <p>Parent Search:
            <asp:TextBox ID="TextBoxSearch" runat="server" Width="207px"></asp:TextBox>
            <asp:Button ID="ButtonSearch" runat="server" Text="Search" />
        </p>
    </ContentTemplate>
 </asp:UpdatePanel>

VB 文件中的代码如下所示,用于处理单击“搜索”按钮,因此 GridView 将根据输入到 TextBox 中的值显示数据。

GridView 也在一个单独的 UpdatePanel 中:

Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click

    GridViewParentsSummary.DataSource = theTableAdapter.GetData(strSearchText)
End Sub

我们想创建一个触发器来更新 GridView,如果这是正确的做法。

这里是 GridView:

    <ContentTemplate> 
        <asp:GridView
            ID="GridViewParentsSummary" 
            runat="server" 
            AllowPaging="True" 
            AllowSorting="True" 
            AutoGenerateColumns="False" 
            DataKeyNames="ID" 
            PageSize="3"
            >

            <Columns>

                <asp:BoundField 
                    DataField="FatherName" 
                    HeaderText="Father's Name" 
                    SortExpression="FatherName" />

                <asp:BoundField 
                    DataField="MotherName" 
                    HeaderText="Mother's Name" 
                    SortExpression="MotherName" />

                <asp:ButtonField 
                    ButtonType="Button" 
                    CommandName="Select" 
                    Text="Select This Parent" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

您能否展示制作刷新 GridView 的正确触发器所需的代码?

【问题讨论】:

  • 感谢您的回复。我将 TextBox 和 Button 移动到与 GridView 相同的 UpdatePanel 中,它现在可以工作了。 :-)

标签: asp.net vb.net gridview triggers updatepanel


【解决方案1】:

如果GridView 在另一个UpdatePanel 中,它也应该在另一个UpdatePanel 更新时更新。默认情况下,UpdatePanel.UpdateMode 属性设置为Always,这将导致页面中的所有UpdatePanel 刷新。

但是,这并不总是您想要的行为,所以很多时候您会将其更改为 Conditional,这意味着只有在触发其中一个触发器时,UpdatePanel 才会被刷新。在这种情况下,您需要在ButtonSearch_Click 方法中添加这一行:

gridUpdatePanel.Update() 'assuming gridUpdatePanel is the UpdatePanel with the grid

有关UpdateMode 属性的更多信息,请查看此处: http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.updatemode.aspx

【讨论】:

  • 感谢您提供非常有帮助的回复。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多