Reputation: 2559
I need help with something, it might be a bit complicated. I call a WebMethod that does somethings from inside a js file. One of the things it does, is that it calls some functions that are located in a class within the app_code.
In that class in app_code I get a GridView Control that is on my page and I need to update it, so I do this:
ActiveGridView.DataSource = (ActiveDataTable).DefaultView;
ActiveGridView.DataBind();
The above works fine when I call the functions in app_code from a regular function, but doesn't update the grid when I call it within the WebMethod.
Any ideas how to solve this problem?
Please note that I CAN'T have the WebMethod return the data and then on client side to update the grid.
Edited
Actually I am using a updatePanel, my aspx code looks like this:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="MappedDataGrid" runat="server" CssClass="pipesTbl">
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRemove" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
And again this works fine when I call the fill grid from an event on the page, but then when I call it after a webMethods calls the class in app_code that is spouse to fill the grid, it doesn't get refilled. Edited again
I think the problem is with this line:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRemove" EventName="Click" />
</Triggers>
I tried changing it to this:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="MappedDataGrid" EventName="DataBinding" />
</Triggers>
But didn't work, I am probably missing something, and since this is all new to me, I didn't know what to do.
What should I be doing now in order to make it work as you said?
Upvotes: 2
Views: 3268
Reputation: 10219
When you call a WebMethod you are not actually loading the page and running through the page lifecycle. Even though your method is part of your page codebehind class, when you call it in this way your controls on the page are not going to be available. It's just a shortcut for making a web service out of a method in your page class.
Based on what you say you want to do, you may want to look at using an UpdatePanel. With them, it IS actually doing a full postback to the page and you can change whatever is necessary with your page controls.
Update
The point with the UpdatePanel is that you won't use your WebMethod anymore. You will just click a normal button that is a trigger for the update panel, and you will update your grid like normal in the code behind (handle the click event of the button and call your code to bind the grid). Since only the grid is in the panel, only it will get refreshed on the screen.
Upvotes: 1
Reputation: 279
If you call a method which manipulates a ASP.NET page out of the lifecycle of that page, you won't view the result. If you want to bind data to a ASP.NET control, it needs to be in the lifecycle of the page, and if you call the method from javascript throught a webmethod, that method will be executed out of the lifecycle.
Try to fill the datagrid with AJAX embedding it in a UpdatePanel.
Upvotes: 1