0

コンボから値を変更したときにラベルを表示する必要がある 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>
    

コンボボックスで選択を変更すると、プログラムは何もしません。何が問題なのか教えてもらえますか?

4

2 に答える 2

0

これはいけません:

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

これがあります:

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

MyServerCall現在、呼び出すメソッドがありません。

あなたが望む方法はOnChangeではなくOnChangedです。また、その方法でアクセスするとエラーが発生LabelMessageします。jQuery を使用した次のような方法でうまくいきます。$('#labelMessage').text(args);

于 2012-10-09T11:10:18.003 に答える
0

RegisterStartupScript を使用して、クライアント スクリプト関数を登録してみてください。

例: Page.ClientScript.RegisterStartupScript(typeof(Page), "MyServerCall", callbackScript, true);

于 2012-10-09T11:22:15.330 に答える