Updatepanel触发gridview中的回发

时间:2014-06-30 17:02:06

标签: asp.net gridview webforms updatepanel postback

我有下一个gridview:

<asp:UpdatePanel ID="upCustomer" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnCustomerSearch" EventName="Click" />
    <asp:AsyncPostBackTrigger ControlID="gvPersons" EventName="PageIndexChanging" />
</Triggers>
<ContentTemplate>
    <asp:TextBox ID="txtCode" runat="server" />
    <asp:Button ID="btnCustomerSearch" runat="server" Text="Search" OnClick="btnCustomerSearch_Click" />
    <asp:GridView ID="gvPersons" runat="server" AutoGenerateColumns="False" AllowPaging="True"
        PageSize="10" GridLines="Vertical" EmptyDataText="No results"
        ShowHeaderWhenEmpty="True" OnRowDataBound="gvPersons_RowDataBound"
        OnPageIndexChanging="gvPersons_PageIndexChanging"
        onselectedindexchanging="gvPersons_SelectedIndexChanging">
        <Columns>
            <asp:BoundField DataField="Personid" />
            <asp:BoundField DataField="Name" HeaderText="Nombre" />
            <asp:BoundField DataField="Phone" HeaderText="Telefono" />
            <asp:BoundField DataField="Email" HeaderText="Email" />
        </Columns>
        <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
    </asp:GridView>
</ContentTemplate>

我需要下一个功能:

  • 当我点击btnCustomerSearch时,我想要一个Ajax更新(工作)
  • 当我点击分页时,我想要一个Ajax更新(工作)
  • 当我点击一行(gvPersons_SelectedIndexChanging)时,我想要一个回发更新,但这不起作用。

是否可以在gridview中的某些事件中获取异步后备并在其他事件中进行回发?

2 个答案:

答案 0 :(得分:2)

在更新面板中插入一个按钮,并为其定义 PostBackTrigger

<asp:UpdatePanel ID="upCustomer" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
    <Triggers>
        ...
        <asp:PostBackTrigger ControlID="PostBackButton" />
    </Triggers>
    <ContentTemplate>
        ...
        <asp:Button ID="PostBackButton" runat="server" Text="Button" OnClick="PostBackButton_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

在您的网格上插入 TemplateField ,如下所示

<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <input type="button" onclick="javascript:__doPostBack('<%= PostBackButton.ClientID %>',<%# ((GridViewRow)Container).RowIndex %>)" value="Select" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

当用户点击网格中的任何按钮时,我们必须手动触发 PostBackButton Click 事件,并传递 RowIndex 作为论点。

在服务器端

protected void PostBackButton_Click(object sender, EventArgs e)
{
    string argument = Request.Form["__EVENTARGUMENT"];
    gvPersons.SelectedIndex = Convert.ToInt32(argument);
}

现在我们只需要隐藏 PostBackButton ...

protected void Page_Load(object sender, EventArgs e)
{
    // ...
    PostBackButton.Style.Add("display", "none");
}

...并禁用页面的事件验证,否则当用户选择行时我们将获得 ArgumentException

<%@ Page Language="C#" ... EnableEventValidation="false" %> 

是。这是可能的。

答案 1 :(得分:0)

如上所述here,您可以尝试这个

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnCustomerSearch" EventName="Click" />
    <asp:AsyncPostBackTrigger ControlID="gvPersons" EventName="PageIndexChanging" />
    <asp:PostBackTrigger ControlID="gvPersons" EventName="SelectedIndexChanging" />
</Triggers>