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
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
[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:
If you only want show or hide Gridview #GrdHeader then you can alway call "BindHeaderGrid" on page load (or the same)
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
Check this.
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));
}
}
In you case i think his might work.
$.ajax({
type: "POST",
dataType: "json",
url: "BindHeaderGrid()",
success: function (data) {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.