从 WebMethod 更新 GridView
我需要一些帮助,可能有点复杂。我调用一个从 js 文件内部执行某些操作的 WebMethod。它所做的事情之一是调用位于 app_code 中的类中的一些函数。
在 app_code 的那个类中,我在页面上获得了一个 GridView 控件,我需要更新它,所以我这样做:
ActiveGridView.DataSource = (ActiveDataTable).DefaultView;
ActiveGridView.DataBind();
当我从常规函数调用 app_code 中的函数时,上面的工作正常,但不更新网格当我在 WebMethod 中调用它时。
有什么想法如何解决这个问题吗?
请注意,我无法让 WebMethod 返回数据,然后在客户端更新网格。
已编辑
实际上我正在使用 updatePanel,我的 aspx 代码如下所示:
<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>
当我从页面上的事件调用填充网格时,这又可以正常工作,但是当我在 webMethods 调用后调用它时,效果很好app_code 中的类是填充网格的配偶,它不会被重新填充。 再次编辑
我认为问题出在这一行:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRemove" EventName="Click" />
</Triggers>
我尝试将其更改为:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="MappedDataGrid" EventName="DataBinding" />
</Triggers>
但是没有用,我可能错过了一些东西,而且由于这对我来说都是新的,所以我没有知道该怎么做。
我现在应该做什么才能使其按照您所说的那样工作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您调用一个在 ASP.NET 页面的生命周期之外操作该页面的方法,您将看不到结果。如果要将数据绑定到 ASP.NET 控件,则它需要位于页面的生命周期中,并且如果您通过 Webmethod 从 javascript 调用该方法,则该方法将在生命周期外执行。
尝试使用 AJAX 填充数据网格,并将其嵌入到 UpdatePanel 中。
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.
当您调用 WebMethod 时,您实际上并未加载页面并运行页面生命周期。即使您的方法是页面代码隐藏类的一部分,当您以这种方式调用它时,页面上的控件将不可用。它只是通过页面类中的方法创建 Web 服务的快捷方式。
根据您所说的想要执行的操作,您可能需要考虑使用 UpdatePanel 。有了它们,它实际上对页面进行了完整的回发,您可以使用页面控件更改任何必要的内容。
更新
UpdatePanel 的要点是您将不再使用 WebMethod。您只需单击一个普通按钮即可触发更新面板,然后您将像后面的代码中那样更新网格(处理按钮的单击事件并调用代码来绑定网格)。由于面板中只有网格,因此只有它会在屏幕上刷新。
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.