簡體   English   中英

GetCallbackEventReference不執行任何操作

[英]GetCallbackEventReference doesn't do anything

我有一個ASP.Net應用程序,當我從組合更改值時必須顯示標簽:

  • 在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); } } 
  • 在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> 

當我在組合框中更改選擇時,程序不執行任何操作。有人可以告訴我這是什么問題嗎?

這不應該:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM