简体   繁体   English

jQuery AJAX和asp.net页面

[英]JQuery AJAX and asp.net page

I have the following setup on an aspx page: 我在aspx页面上进行了以下设置:

<asp:Content ID="contentBody" ContentPlaceHolderID="body" Runat="Server">
    <div class="Container-WithoutSideBar">
        <asp:Label ID="TextTitle" runat="server" AssociatedControlID="ArticleTitle" Text="Title " />
        <asp:TextBox ID="ArticleTitle" TextMode="SingleLine" runat="server"></asp:TextBox>       
...
</asp:Content>

and the following JQuery AJAX: 以及以下JQuery AJAX:

function fnGetArticleHTML()
{
    var articleText = JSON.stringify(tinyMCE.activeEditor.getContent());
    $.ajax({
        type: "POST",
        url: 'adminpageadd.aspx/SubmitArticleHTML',
        data: "{ 'ArticleHTML': " + articleText + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert("Article saved");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            //alert("Error Saving Article");                    
            alert(xhr.responseText);
       }
    });            
    return false;
}

Which all works fine. 一切正常。

And the server side code: 和服务器端代码:

[System.Web.Services.WebMethod]
public static string SubmitArticleHTML(string ArticleHTML)
{        
    news_adminpageadd addArticle = new news_adminpageadd();

    return addArticle.SubmitArticle(ArticleHTML);
}

private string SubmitArticle(string ArticleHTML)
{
    return ArticleTitle.Text;
}

The problem is, that when I try and return the Article.Text ,which is the content of the asp:TextBox I get the following exception: 问题是,当我尝试返回Article.Text ,这是asp:TextBox的内容时,出现以下异常:

{"Message":"Object reference not set to an instance of an object.","StackTrace":"   at news_adminpageadd.SubmitArticle(String ArticleHTML) in .....\news\\adminpageadd.aspx.cs:line 69\r\n   at news_adminpageadd.SubmitArticleHTML(String ArticleHTML) in ....\news\\adminpageadd.aspx.cs:line 64","ExceptionType":"System.NullReferenceException"}

Which is essentially saying that: 本质上说:

ArticleTitle.Text;

is not set not an instance of an object. 未设置不是对象的实例。

I know this is not the case, as on Page_load it works fine. 我知道情况并非如此,因为在Page_load上工作正常。

I assume that somehow the interaction with the WebMethod is the problem, but how do I solve it? 我以某种方式认为与WebMethod的交互是问题所在,但是我该如何解决呢?

Setting the textbox to client side and passing it through int he AJAX will not help for me, as I need it server side to load the correct values. 将文本框设置为客户端并通过AJAX传递对我来说无济于事,因为我需要在服务器端加载正确的值。

I have tried finding the control, but this still gives the same exception. 我试图找到控件,但这仍然给出相同的异常。

Any ideas? 有任何想法吗?

I passed the variable to the server side like this (I am sure I can tidy it up a bit): 我像这样将变量传递给服务器端(我敢肯定我可以整理一下):

function fnGetArticleHTML()
{
    var articleText = JSON.stringify(tinyMCE.activeEditor.getContent());
    var ArticleTitle = JSON.stringify($('#<%=ArticleTitle.ClientID%>').val());
    var ArticleAuthor = JSON.stringify($('#<%=AuthorName.ClientID%>').val());
    var ArticlePosition = JSON.stringify($('#<%=AuthorPosition.ClientID%>').val());
    var ArticlePhonNo = JSON.stringify($('#<%=AuthorPhone.ClientID%>').val());
    var ArticleEmail = JSON.stringify($('#<%=AuthorEmail.ClientID%>').val());
    var ArticleWebsite = JSON.stringify($('#<%=AuthorWebsite.ClientID%>').val());
    var ArticleCategory = JSON.stringify($('#<%=ArticleCategories.ClientID%>').attr('value'));

    $.ajax({
        type: "POST",
        url: 'adminpageadd.aspx/SubmitArticleHTML',
        data: "{ 'ArticleHTML': " + articleText + ", 'ArticleTitle':" + ArticleTitle + ", 'ArticleAuthor':" + ArticleAuthor + ", 'ArticlePosition':" + ArticlePosition + ", 'ArticlePhonNo':" + ArticlePhonNo + ", 'ArticleEmail':" + ArticleEmail + ", 'ArticleWebsite':" + ArticleWebsite + ", 'ArticleCategory':" + ArticleCategory + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert("Article saved: " + response.d);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Error Saving Article");                                        
       }
    });            
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM