0

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

8
  • That's a wrong way of doing that. You cannot force a ASPX Datagrid to Bind() from Javascript. That would require a postback. I suggest you look into scriptmanager and updatepanel. Commented May 23, 2017 at 4:29
  • @uɐpuɐɥƆ, I don't want to call javascript function from codebehind. I need .cs method to call from javascript function. Commented May 23, 2017 at 4:34
  • refer stackoverflow.com/questions/7089760/… Commented May 23, 2017 at 4:36
  • @uɐpuɐɥƆ, I tried that one also but my function is not calling Commented May 23, 2017 at 4:39
  • Debug in both .cs and javascript and Mention what error you are getting Commented May 23, 2017 at 4:40

2 Answers 2

0

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:

  1. If you only want show or hide Gridview #GrdHeader then you can alway call "BindHeaderGrid" on page load (or the same)

  2. 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

Sign up to request clarification or add additional context in comments.

3 Comments

which your choice?
I need when i click image(+) i need to load and display gridview. when image (-) click hide gridview
ServerSide: aspx.cs protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindHeaderGrid() } }
0

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) {

                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.