GLP
GLP

Reputation: 3675

How to rebind a gridview after a web service call?

I have a page which has a GridView placed in an update panel. Each row in the GridView has a update button, when clicked, a modal dialog will pop up, where you could edit the corresponding row and save the record to the database through a web service call. Following is the aspx and jquery code. My question, how do I reload the GridView after update the record? I tried the __doPostBack, it didn’t seem work.

<div id="dvMainContainer">
        <asp:UpdatePanel ID="upMainPanel" runat="server" RenderMode="Inline" UpdateMode="Conditional">
            <ContentTemplate>

                <table summary="content" id="Table3">
                    <tr> 
                        <td>   
                            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                                AllowSorting="false" AutoGenerateColumns="False" PageSize="20" 
                                onpageindexchanging="GridView1_PageIndexChanging">
                            <Columns>
                                <asp:TemplateField >
                                    <ItemTemplate>
                                        <div><input type="button" value="update" /></div>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField >
                                    <HeaderTemplate>Vendor<br />
                                        <asp:DropDownList ID="ddlVendor" runat="server" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="OnVendorChanged">
                                            <asp:ListItem Text="- Select a Vendor -" Value=""></asp:ListItem>
                                        </asp:DropDownList>
                                    </HeaderTemplate>
                                    <ItemTemplate><%#Eval("Vendor") %></ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Name" >
                                    <ItemTemplate><%#Eval("Name")%></ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Description" >
                                    <ItemTemplate><%#Eval("Description")%></ItemTemplate>                
                                </asp:TemplateField>
                            </Columns>
                            </asp:GridView>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </div>

    <div id="dvUpdatePrompt" style="display: none;" runat="server">
        <table>
            <tr>
                <td>Vendor:</td>
                <td><select name="VendorList" id="VendorList"></select></td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><input type="text" id="txtName"/></td>
            </tr>
            <tr>
                <td>Description:</td>
                <td><input type="text" id="txtDescription"/></td>             
            </tr>
        </table>
    </div>

    this.BindEvents = function () {
        var myself = this;

        $(myself.get_element()).find("input[value='update']").each(
            function () {
                var input = $(this);

                if (input.closest('tr').find('td')[1].innerText.replace('&nbsp;', '') != '') {
                    input.click(
                        function () {
                            myself.UpdateButtonClicked(this);
                        }
                    );
                }
                else {
                    input.attr('disabled', 'disabled');
                }
            }
        );
    }
    this.UpdateButtonClicked = function (button) {
        var myself = this;
        var btns = {};
        var Vendor = "";
        var Name = "";
        var Description = "";

        …
        btns["Save"] = function () {
            myself.UpdateVendor(button);                   
        };

        btns["Cancel"] = function () { $("#dvUpdatePrompt").dialog('close'); };

        …
        $("#dvUpdatePrompt").dialog({
            modal: true,
            buttons: btns,
            width: 500,
            height: 300
        });
    }

    this.UpdateVendor = function (button) {
        var myself = this;

        …
        var parmIn = "…";

        myself.Utilities.CallWebService('WebService.asmx/UpateVendor', parmIn,
            function (result) {
                $("#dvUpdatePrompt").dialog('close');
                //TODO rebind GridView1
                __doPostBack('$("#upMainPanel")', '');
            }
        );
    }

Update: I added following button and trigger, and I call the click(), but didn't seem work.

<div id="dvMainContainerTonnyMadeUp">
        <asp:Button ID="btnReBind" runat="server" Text="ReBind" Visible="false" />
        <asp:UpdatePanel ID="upMainPanel" runat="server" RenderMode="Inline" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnReBind" EventName="click"/>
            </Triggers>

myself.Utilities.CallWebService('DBMaintenanceService.asmx/UpateXrefProductCode', parmIn,
            function (result) {
                $("#dvUpdatePrompt").dialog('close');
                //TODO rebind GridView1
                $("#btnReBind").click();
            }

Upvotes: 0

Views: 407

Answers (1)

Abhitalks
Abhitalks

Reputation: 28437

Since you are already wrapping your gridview in update panel, one quick solution would be to have a button (say refresh).

Specify this button as a trigger to the gridview. Call click() on this button in your jquery code after the service call completes.

<asp:UpdatePanel ID="upMainPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnRefresh" />
</Triggers>
...
<asp:Button ID="btnRefresh" style="display:none;" UseSubmitBehavior="false" runat="server" OnClick="btnRefresh_Click" />
<asp:GridView...
...

And in your code behind:

Protected Sub btnRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
    'Your routine to populate / databind gridview
End Sub

Note: The above handler is required only if you are databinding your gridview programmatically.

And in your javascript/jquery code:

function callRefresh() {
    // if your clientid mode is static then
    document.getElementById('btnRefresh').click();
    // otherwise if id is mangled then something like this
    document.getElementById('ctl00_ContentPlaceHolder1_btnRefresh').click();
}

Being a trigger, the button click will cause a partial postback thereby causing the page life cycle to run on the server. If you are using a datasource in markup (or databinding in init) then nothing needs to be done more. Otherwise, in the button server-click you need to databind the gridview again.

Upvotes: 1

Related Questions