简体   繁体   English

GetCallbackEventReference不执行任何操作

[英]GetCallbackEventReference doesn't do anything

I have an ASP.Net application that must show a label when i changed value from a combo: 我有一个ASP.Net应用程序,当我从组合更改值时必须显示标签:

  • in cs file: 在cs文件中:

      public partial class WebFormAJAXControls : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler { string _callbackArg; public void RaiseCallbackEvent(string eventArgument) { _callbackArg = eventArgument; } public string GetCallbackResult() { return _callbackArg; } protected void Page_Load(object sender, EventArgs e) { //register the name of the client side function that will be called by the server string callbackRef = Page.ClientScript.GetCallbackEventReference(this, "args", "ClientCallbackFunction",""); //define a function used by client to call server string callbackScript = " function MyServerCall(args){alert ('1'); " + callbackRef + " ; }"; //register the client function to the page Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyServerCall", callbackScript, true); } } 
  • in aspx file: 在aspx文件中:

      <head runat="server"> <title></title> <script type="text/javascript"> function ClientCallbackFunction(args) { alert(args); LabelMessage.innerText = args; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownListChoice" runat="server" OnChanged="MyServerCall(DropDownListChoice.value)"> <asp:ListItem>Choise 1</asp:ListItem> <asp:ListItem>Choise 2</asp:ListItem> <asp:ListItem>Choise 3</asp:ListItem> </asp:DropDownList> <br /><br /> <asp:Label ID="labelMessage" runat="server"></asp:Label> </div> </form> </body> 

When I change selection in the combobox the program doesn't do anithing.Can somebody tell me what's the problem? 当我在组合框中更改选择时,程序不执行任何操作。有人可以告诉我这是什么问题吗?

Shouldn't this: 这不应该:

function ClientCallbackFunction(args) { 
    alert(args); 
    LabelMessage.innerText = args; 
}

be this: 是这样的:

function MyServerCall(args) { 
    alert(args); 
    LabelMessage.innerText = args; 
}

Currently you don't have a MyServerCall method to invoke. 当前,您没有要调用的MyServerCall方法。

The method you want is OnChange , not OnChanged . 您想要的方法是OnChange ,而不是OnChanged You will also get an error accessing LabelMessage that way. 您还会通过这种方式访问LabelMessage遇到错误。 Something like this with jQuery would do the trick: $('#labelMessage').text(args); jQuery这样的东西可以解决问题: $('#labelMessage').text(args);

Try using RegisterStartupScript to register client script function. 尝试使用RegisterStartupScript注册客户端脚本功能。

Eg: Page.ClientScript.RegisterStartupScript(typeof(Page), "MyServerCall", callbackScript, true); 例如:Page.ClientScript.RegisterStartupScript(typeof(Page),“ MyServerCall”,callbackScript,true);

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

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