[英]How to call C# code from javascript function
In my application I need to call aspx.cs method to bind Gridview from javascript function call how can i do this I searched and found some codes but it didn't work for me 在我的应用程序中,我需要调用aspx.cs方法来从javascript函数调用中绑定Gridview,我该怎么做?我搜索并找到了一些代码,但是它对我不起作用
I tried code: 我尝试了代码:
Client Side: 客户端:
<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 服务器端:aspx.cs
[WebMethod]
public void BindHeaderGrid()
{
GrdHeader.DataSource = ds.Tables[0];
GrdHeader.DataBind();
}
Thank you 谢谢
You can not do this - Call a function in Server side from client side. 您不能这样做-从客户端在服务器端调用一个函数。
I seen your code and I have 2 options for you: 我看过您的代码,有2个选项供您选择:
If you only want show or hide Gridview #GrdHeader then you can alway call "BindHeaderGrid" on page load (or the same) 如果您只想显示或隐藏Gridview #GrdHeader,则可以始终在页面加载时调用“ BindHeaderGrid”(或相同名称)
If you want reload Gridview when user click to #img2 then you may by split GridView #GrdHeader to a page and embeaded it to this page by a iframe tag, when use click to img #img2 only reload the iframe tag 如果要在用户单击#img2时重新加载Gridview,则可以将GridView #GrdHeader拆分为一个页面,并通过iframe标记将其嵌入到此页面,当使用click到img#img2时,仅重新加载iframe标记
Check this. 检查一下。
JavaScript 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 C#Web服务
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));
}
}
In you case i think his might work. 在你的情况下,我认为他可能会起作用。
$.ajax({
type: "POST",
dataType: "json",
url: "BindHeaderGrid()",
success: function (data) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.