2012-02-07 41 views
0

刚开始使用AJAX,并在Microsoft®70515书中尝试了一个简单示例。然而,代码似乎没有工作,我不明白为什么不 - 因为它似乎确定。无法让AJAX代码起作用

  • 编辑:由于某种原因,该代码的一部分没有得到公布,(即使我写这现在的代码看起来怪异,这就像我不能发布我的代码?)我”现在已经有fixad了 - 但是投票结果如何呢?我真的不知道我的问题是什么愚蠢的。请解释。

希望有人能发现的问题,并帮助我在这里:)

标记的.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="AjasxTest._Default" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 

<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> 


<br /><br /> 

<script type="text/javascript"> 
    function ClientCallbackFunction(args) { 
     window.LabelMessage.innerText = args; 
    } 
</script> 

</asp:Content> 
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="FooterContent"> 
<asp:DropDownList ID="DropDownListChoice" runat="server" OnChange="MyServerCall(DropDownListChoice.value)"> 
<asp:ListItem>Choice 1</asp:ListItem> 
<asp:ListItem>Choice 2</asp:ListItem> 
<asp:ListItem>Choice 3</asp:ListItem> 
</asp:DropDownList> 
<asp:Label ID="LabelMessage" runat="server"></asp:Label> 
</asp:Content> 

代码隐藏:

namespace AjasxTest 
{ 
    public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      string callbackRef = Page.ClientScript.GetCallbackEventReference(this, "args", "ClientCallbackFunction", ""); 

      string callbackScript = String.Format("function MyServerCall(args) {{{0};}}", callbackRef); 

      Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"MyServerCall", callbackScript, true); 
     } 

     public string GetCallbackResult() 
     { 
      return _callbackArgs; 
     } 

     string _callbackArgs; 

     public void RaiseCallbackEvent(string eventArgument) 
     { 
      _callbackArgs = eventArgument; 
     } 
    } 
} 
+1

嗨,你可以发布ajax请求创建功能。在解释你的问题时要更加清楚,不要直言不讳地说。只要说明你的期望是什么以及它的行为如何? – AmGates 2012-02-07 09:08:59

+0

对不起,我不知道我在做什么错,但我的代码不会发布:(我已经尝试了四次,但每次都会搞砸。以前从未遇到过这个问题。 – 2012-02-07 11:33:43

回答

0

你的JS函数被调用ClientCallbackFunction但您在DropDownList OnChange事件中将其称为MyServerCall

<script type="text/javascript"> 
    function ClientCallbackFunction(args) { 
     window.LabelMessage.innerText = args; 
    } 
</script> 

...

<!-- Call correct JS function in OnChange --> 
<asp:DropDownList ID="DropDownListChoice" runat="server" OnChange="ClientCallbackFunction(DropDownListChoice.value)"> 

...

+0

对不起,我不知道我在做什么我做错了,但我的代码不会发布:(我已经尝试过四次,但每次都会搞砸。以前从未遇到过这个问题。 – 2012-02-07 11:34:16

+0

@Daniela我已经格式化了这个问题,现在我可以看到你是什么了试图做...和我的回答不回答你的问题... – 2012-02-07 11:47:20

+0

谢谢格式:) – 2012-02-07 13:20:16

0

你的代码是非常接近,但问题是在你试图访问服务器端对象(如标签和DropDownList的事实)从客户端。

例如,asp:Label控件在呈现给客户端的Web浏览器时,实际上是一个HTML div。 Visual Studio中标签的ID可能是“LabelMessage”,但根据页面和控件的布局,客户端的ID(例如,单击FireFox或IE中的视图源的用户)可以生成为“FooterContent_LabelMessage1”或类似的东西。

ASP.net控件确实带有一个属性,您可以使用该属性访问JavaScript中生成的ID,该属性可以通过YourControl.ClientID访问。

我对这些代码进行了这些更改,并且已经能够使其工作。我还在JavaScript中添加了一些空引用检查,以确保我们试图引用的对象实际上存在。注意我已经在一个空白的,全新的ASP.net窗体应用程序中测试过了。

下面是默认页(前端)更新的代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="TheAnswer._Default" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <br /> 
    <br /> 
    <script type="text/javascript"> 
     function ClientCallbackFunction(args) { 
      var labelMessage = $get("<%= LabelMessage.ClientID %>"); 
      if (labelMessage) { 
       labelMessage.innerText = args; 
      } 
     } 
     function MyServerCallWrapper() { 
      var dropDown = $get("<%= DropDownListChoice.ClientID %>"); 
      if (dropDown) { 
       MyServerCall(dropDown.value); 
      } 
     } 
    </script> 
</asp:Content> 
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="FooterContent"> 
    <asp:DropDownList ID="DropDownListChoice" runat="server" onchange="javascript:MyServerCallWrapper();"> 
     <asp:ListItem>Choice 1</asp:ListItem> 
     <asp:ListItem>Choice 2</asp:ListItem> 
     <asp:ListItem>Choice 3</asp:ListItem> 
    </asp:DropDownList> 
    <asp:Label ID="LabelMessage" runat="server"></asp:Label> 
</asp:Content> 

注意$的get()函数是一个内置在ASP.net(不JQuery的)功能是文件的简写。 getElementById() - 它们是可以互换的并且执行完全相同的操作。您需要将引号中的ClientID传递给JavaScript中的任何一种方法,并且它会返回对您尝试访问的对象的引用。

只是为了好玩,我修改的后端功能之一,所以你知道,回调是在服务器上处理:

public string GetCallbackResult() 
{ 
    return _callbackArgs + " from the server!"; 
} 

瞧!这对我的作品 -

Screenshot of Ajax callback result

我希望它为你工作太多,我希望这显然问题出在哪里了;你的大部分例子都是完美的工作/设置。