2

I am getting my inner html through following JavaScript AJAX function: Following Code is not working... I also tried ajax with no success.

jQuery (AJAX Code)

$('#Button1').click(function () {
        var HTML = document.getElementById("selected-column-1").innerHTML;
        alert(HTML);
        $.ajax({
            type: "post",
            url: "Default.aspx/save_rec",
            data: {"HTML":HTML},
            dataType: "json",
            success: function (result) {
                //alert('sucess' + result.d);
            }
        });
    });

But neither of these approaches hit my this method.

C#

[WebMethod]
    public static string save_rec(string HTML)
    {          
        return "";
    }

But this approache not hit my C# method

console error

c# message invalid object passed in member name expected
  • Why do you need it in your code behind normally its the other way around code behind value in html? Maybe explain what you are trying to accomplish and then we can come up with a good solution for you... – brso05 Sep 26 '14 at 12:42
  • actually i want to save the inner html of the form and want to save this in my database, and then render that inner html in another part, normally user came design his form save form(we do inner html saving to database), then user open a form again(that is designed by him). enter the values and save them in database again – Caffeine addicted Sep 26 '14 at 12:47
  • You can use jquery ajax calls ,create a Static Webmethod in codebehind and call it by jquery ajax ,in which by setting values parameter you can send your HTML in codebehind easily . – LearningAsp Sep 26 '14 at 12:51
  • i used ajax, but i am not succeeded, my ajax call doesnt hit the function in ,y aspx page – Caffeine addicted Sep 26 '14 at 12:53
  • Can you put your ajax code here? – LearningAsp Sep 26 '14 at 12:54
  • I posted my answer ,you may look into that – LearningAsp Sep 26 '14 at 13:07

3 Answers3

2
 using System.Web.Services;
 [WebMethod ]
 public static void GetTableColumn(string dataa)
 {
     //your code
 }

use it like this

LearningAsp
  • 351
  • 1
  • 2
  • 12
0

Refer: How to use __doPostBack()

Create a method which will be called on in javascript to send postback to code behind:

function testMe(params) {
    var btnID= '<%=MyButton.ClientID %>';          
    __doPostBack(btnID, params);
}

Reference:
How to call Postback from Javascript
_doPostBack function in javascript - asp.net postback mechanism
Generating Client-Side Script for Postback

If you do not want to generate postback then you can create web methods on the code behind and you can directly call them from java script code:

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [WebMethod]
    public static string Name()
    {
        string Name = "Hello Stack";
        return Name;
    }
}

Javascript calling:

<script type='text/javascript'>
        function GetName() {

            PageMethods.Name(Success, Failure);
        }
        function Success(result) {
            alert(result);
        }
        function Failure(error) {
            alert(error);
        }
    </script>

Refer the below:
How to call Server Side function from Client Side Code using PageMethods in ASP.NET AJAX
Calling Server Side Function From JavaScript in ASP.Net
Call ASP.Net Page Method using jQuery AJAX Example
Calling an ASP.NET C# Method (Web Method) Using JavaScript

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

Solved after making changes in the ajax call method

$('#Button1').click(function () {
        var HTML = document.getElementById("selected-column-1").innerHTML;
        Senddata = { "HTML": HTML};
        $.ajax({
            type: "post",
            url: "Default.aspx/save_rec",
           data: JSON.stringify(Senddata),
           contentType: "application/json; charset=utf-8",
           dataType: "json", 
    success: function (result) {
                //alert('sucess' + result.d);
            }
        });
    });