Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
4.00/5 (4 votes)
See more:
I know this has been asked too much and there are these on the net:
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/[^]
http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/[^]
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/[^]

I went through all of those before posting

Here's my scenario,
I have a GirdView in an UpdatePanel on one page, and I need to update it from another page.
Sounds simple, but obviously, when I use a static webmethod, I cannot refer to the gridview on the UpdatePanel,

Here's some code:
Page1.aspx
ASP.NET
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script>
        function update() {
            jQuery.ajax({
                url: 'Page2.aspx/GetData',
                type: "POST",
                data: "{'userid':" + userid + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert(data.d);
                    
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1"  runat="server">
        <div>
    <input type="button" value="click me"  önclick="update();" />
    </div>
    <div>
    </form>
</body>


Page2.aspx: - This has the grid and the static method
ASP.NET
<head  runat="server">
    <title></title>
    <script language=javascript>
        function update() {
            __doPostBack('UpdatePanel1', ''); //I can't call this!!
        }
    </script>
</head>
<body>
    <form id="form1"  runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
    <asp:UpdateProgress ID="UpdateProgress1" AssociatedUpdatePanelID=UpdatePanel1 runat=server>
    <ProgressTemplate>
    WAIT!!!!
    </ProgressTemplate>
    </asp:UpdateProgress>
    <asp:UpdatePanel runat="server" ID="UpdatePanel1" onload="UpdatePanel1_Load">
    <ContentTemplate>
    <asp:GridView id=gv runat=server AutoGenerateColumns=true>
    </asp:GridView>
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


page2.aspx.cs
C#
[WebMethod()]
       public static string GetData(int userid)
       {
           //UPDATE the GridView HERE!
       }
       protected void UpdatePanel1_Load(object sender, EventArgs e)
       {
           makeatableandbind();
       }


I cant call the UpdatePanel1_Load or cannot refer to the GridView from the static method

Im sure you guys here have done something like this

Need some help
Posted
Comments
sahabiswarup 9-Nov-12 4:37am    
Good Ques 5 from me.
waiting for positive answer.

1 solution

The solution is to use __doPostBack, pretty simple
I didn't have time to share the solution....
here goes
Page 1
has gridview in an updatepanel
page 2
has some action, after which it should update the gridview on page 1

Page1 has some javascript like
JavaScript
function refresh()
{
 var btnid = '<%=btnUpdate.UniqueID%>'
 __doPostBack(btnid, "OnClick");
}

the the update panel with a gridview and a button
C#
<asp:updatepanel id="UpdatePanel1" runat="server" onload="UpdatePanel1_Load" xmlns:asp="#unknown">
    <contenttemplate>
        <asp:gridview....>
    </asp:gridview....></contenttemplate>
    <triggers>
        <asp:asyncpostbacktrigger controlid="btnUpdate" eventname="Click" />
    </triggers>
</asp:updatepanel>
<asp:button id="btnUpdate" runat="server" style="display:none;" xmlns:asp="#unknown" />


and on page 2
JavaScript
<script language="Javascript">
    function RefreshParent() {
        parent.refresh();
    }
</script>

call this JavaScript function like:
C#
ClientScript.RegisterClientScriptBlock(this.GetType(), "afterSave", "refresh();", true);



Hope this helps
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900