关于asp.net:当用户单击UpdatePanel中的按钮时,触发以刷新DataView

Trigger to refresh DataView when user clicks a button inside an UpdatePanel

我们在UpdatePanel中具有以下代码。

1
2
3
4
5
6
7
8
9
10
11
12
 <asp:UpdatePanel
    ID="UpdatePanelSearch"
    runat="server"
    UpdateMode="Conditional">

    <ContentTemplate>
        <p>Parent Search:
            </asp:TextBox>
           
        </p>
    </ContentTemplate>
 </asp:UpdatePanel>

VB文件中的代码看起来像这样,可以单击"搜索"按钮,因此GridView将基于在TextBox中输入的值来显示数据。

GridView也位于单独的UpdatePanel中:

1
2
3
4
Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click

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

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

这是GridView:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    <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的正确触发器所需的所需代码吗?


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

但是,并非总是如此,因此您经常将其更改为Conditional,这意味着UpdatePanel仅在触发了其中一个触发器时才会刷新。在这种情况下,您需要在ButtonSearch_Click方法中添加以下行:

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

有关UpdateMode属性的更多信息,请参见此处:
http://msdn.microsoft.com/zh-cn/library/system.web.ui.updatepanel.updatemode.aspx