GetCallbackEventReference没有做任何事情
问题描述:
我有一个ASP.Net应用程序时我改变从一个组合值,它必须显示一个标签:在CS文件GetCallbackEventReference没有做任何事情
-
:
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); } }
-
在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>
当我改变选择离子在组合框程序不anithing.Can有人告诉我有什么问题?
答
不应该这样:
function ClientCallbackFunction(args) {
alert(args);
LabelMessage.innerText = args;
}
是这样的:
function MyServerCall(args) {
alert(args);
LabelMessage.innerText = args;
}
目前你没有MyServerCall
方法来调用。
你想要的方法是OnChange
而不是OnChanged
。您也将以这种方式访问LabelMessage
时出错。像这样的jQuery会做的伎俩:$('#labelMessage').text(args);
答
尝试使用RegisterStartupScript来注册客户端脚本功能。例如:Page.ClientScript.RegisterStartupScript(typeof(Page),“MyServerCall”,callbackScript,true);
不工作:( – user1577242