Updatepanel trigger postback in gridview

asp.netgridviewpostbackupdatepanelwebforms

I have the next 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>

I need the next funcionality:

  • When I click btnCustomerSearch, I want an Ajax update (works)
  • When I click pagination, I want an Ajax update (works)
  • When I click a row (gvPersons_SelectedIndexChanging), I want a postback update, but this doesnt work.

Is posible to get an asyncpostback in some events in a gridview and postback in anothers?

Best Answer

Insert a Button inside your update panel and define a PostBackTrigger for it

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

Insert a TemplateField on your grid like this

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

When the user clicks on any button inside the grid, we will have to manually fire the Click event of the PostBackButton passing the RowIndex as argument.

On the server side

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

Now we just have to hide the PostBackButton...

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

...and disable the event validation of the page, otherwise we will get an ArgumentException when the user selects a row

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

Yes. It is possible.

Related Topic