在我的应用程序中,我需要调用aspx.cs方法从javascript函数调用绑定Gridview我怎么能这样做我搜索并找到了一些代码,但它对我不起作用
我试过代码:
客户端:
<script>
function MyHeader() {
PageMethods.BindHeaderGrid(); //I tried this one also
var x = document.getElementById('HeaderDiv');
if (x.style.display === 'none') {
x.style.display = 'block';
img2.src= "minus.gif";
} else {
x.style.display = 'none';
img2.src= "plus.gif";
}
}
</script>
<img id='img2' width="9px" border="0" src="plus.gif" onclick="MyHeader()"/> Header <div id="HeaderDiv" style="display:none">
<asp:GridView ID="GrdHeader" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="SenderID" HeaderText="SenderID" />
<asp:BoundField DataField="ReceiverID" HeaderText="ReceiverID" />
<asp:BoundField DataField="Transactiondate" HeaderText="Transactiondate" />
<asp:BoundField DataField="RecordCount" HeaderText="RecordCount" />
<asp:BoundField DataField="DispositionFlag" HeaderText="DispositionFlag" />
</Columns>
</asp:GridView></div>
ServerSide:aspx.cs
[WebMethod]
public void BindHeaderGrid()
{
GrdHeader.DataSource = ds.Tables[0];
GrdHeader.DataBind();
}
谢谢
答案 0 :(得分:0)
您不能这样做 - 从客户端调用服务器端的功能。
我看到了你的代码,我有两个选择:
如果您只想显示或隐藏Gridview #GrdHeader,那么您可以随时调用&#34; BindHeaderGrid&#34;在页面加载(或相同)
如果您想在用户点击#img2时重新加载Gridview,那么您可以将GridView #GrdHeader拆分为一个页面并通过iframe标记将其嵌入到此页面,当使用时单击以img#img2仅重新加载iframe标签
答案 1 :(得分:0)
检查一下。
JavaScript
$.ajax({
type: "POST",
dataType: "json",
data: {data: data}, // if no data available leave this out
url: "example.asmx/exampleMethod", //if you wanna call a function in the page just include the function name in here
success: function (data) {
// do something if the function is success
}
});
C#Webservice
public class Example: System.Web.Services.WebService
{
[WebMethod]
public void exampleMethod(Parameters)
{
// Something you wanna do
//If you wanna return something to javascript
Context.Response.ContentType = "text/HTML";
var js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(returnDataObject));
}
}
在你的情况下,我认为他可能会工作。
$.ajax({
type: "POST",
dataType: "json",
url: "BindHeaderGrid()",
success: function (data) {
}
});