2016-03-23 84 views
0

我有一個aspx頁面上的以下設置:JQuery的AJAX和asp.net頁面

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

及以下的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; 
} 

這一切工作正常。

而服務器端代碼:

[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; 
} 

的問題是,當我嘗試返回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"} 

其實質上是這樣說的:

ArticleTitle.Text; 

未設置不是一個對象的實例。

我知道情況並非如此,因爲在Page_load上它工作正常。

我認爲與WebMethod的交互是問題,但我該如何解決它?

將文本框設置爲客戶端,並通過int傳遞他AJAX不會幫助我,因爲我需要它在服務器端加載正確的值。

我試過找到控件,但是這仍然給出了相同的例外。

任何想法?

+0

您無法訪問Web窗體控件(即'的')來自Web方法。您必須將值傳遞給您的Web方法。 – zgood

回答

0

我傳遞的變量到服務器端這樣的(我相信我能夠把它打掃乾淨了一下):

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; 
}