用于将项目列表提取到GridView中的JavaScript代码
我有一个ASP.NET Web应用程序。在用于将项目列表提取到GridView中的JavaScript代码,javascript,asp.net,gridview,pagemethods,Javascript,Asp.net,Gridview,Pagemethods,我有一个ASP.NET Web应用程序。在Page\u Load上创建一个包含项目的网格视图。我还有一个和一个搜索按钮。我正在使用PageMethods来摆脱回发。 这个想法是,当点击按钮时,它将调用javascript函数,然后它将调用WebMethod。WebMethod根据搜索条件返回项目列表 如何获取这些项并用它们填充GridView 换句话说,它类似于一个局部更新按钮单击它应该只更新GridView 以下是WebMethod的代码: [WebMethod] public sta
Page\u Load
上创建一个包含项目的网格视图。我还有一个
和一个搜索按钮。我正在使用PageMethods
来摆脱回发。
这个想法是,当点击按钮时,它将调用javascript函数,然后它将调用WebMethod
。WebMethod
根据搜索条件返回项目列表
如何获取这些项并用它们填充GridView
换句话说,它类似于一个局部更新按钮单击它应该只更新GridView
以下是WebMethod的代码:
[WebMethod]
public static List<Item> SearchResults(string text)
{
ItemFunctions item_functions = new ItemFunctions();
List<Item> items = new List<Item>();
items = item_functions.searchResults(text);
return items;
}
[WebMethod]
公共静态列表搜索结果(字符串文本)
{
ItemFunctions item_functions=新的ItemFunctions();
列表项=新列表();
items=item_函数。搜索结果(文本);
退货项目;
}
这是GridView、按钮和文本框:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<asp:TextBox ID="search_textbox" runat="server" />
<input type="button" id="Button1" value="Search" onclick="SearchResults();" />
<div id="searchResults_GridView" >
<asp:GridView ID="GridView1" AllowPaging="true" GridLines="Both" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
Width="740px"
CausesValidation="False">
<Columns>
<asp:BoundField DataField="item_name" HeaderText="item_name" />
<asp:BoundField DataField="item_description" HeaderText="item_description" />
<asp:BoundField DataField="item_quantity" HeaderText="item_quantity" />
<asp:BoundField DataField="category_name" HeaderText="category_name" />
<asp:BoundField DataField="type_name" HeaderText="type_name" />
<asp:BoundField DataField="item_available" HeaderText="item_available" />
<asp:BoundField DataField="item_serial_number" HeaderText="item_serial_number" />
<asp:BoundField DataField="item_permission_status" HeaderText="item_permission_status" />
<asp:BoundField DataField="item_location" HeaderText="item_location" />
<asp:CommandField ShowSelectButton="true" SelectText="View" />
</Columns>
</asp:GridView>
</div>
这是在单击按钮时调用的SearchResults函数:
<script>
function SearchResults() {
PageMethods.SearchResults(OnSucceeded, OnFailed);
}
function OnSucceeded(result, userContext, methodName) {
}
function OnFailed(error, userContext, methodName) {
alert("An error occured :(");
}
</script>
函数SearchResults(){
PageMethods.SearchResults(OnSucceeded,OnFailed);
}
函数OnSucceeded(结果、用户上下文、方法名){
}
函数OnFailed(错误、userContext、methodName){
警报(“发生错误:(”);
}
我的javascript技能很差,我还在学习中,否则理论上我认为其余的代码应该可以工作。你可以用javascript完成这项工作,但此时需要你转储GridView的html(因为在GridView呈现后,它只是html)重新创建一个简单的HTML表。虽然这可能是一个首选解决方案,但在一次完整的回发中,您将丢失所有数据,并且需要重新运行javascript 您可以研究使用UpdatePanel实现此功能。有关使用UpdatePanel的帮助,请参阅以下链接:
保持.NET WebForms的风格,我建议您研究updatepanel控件。这不完全是您想要的,但仍然是一个“部分更新”。按照您希望的方式进行操作有点“黑客”WebForms方式,可能会在刷新时引起麻烦……是的,更新面板这次似乎完成了这项工作。谢谢:)最后,我用更新面板完成了它。谢谢你的评论。