Stopping onclick from firing when onclientclick is false?

前端 未结 5 843
我在风中等你
我在风中等你 2020-11-30 10:45

Is it possible to use the onclientclick property of a button to do a clientside check. If the check returns true, then fire the onclick

5条回答
  •  一生所求
    2020-11-30 11:24

    In the server page create the button:

    var button1 = new Button();  
    button1.ServerClick += new EventHandler(button1_ServerClick);
    button1.OnClientClick = SetJsForSaveBtn();
    button1.Attributes.Add("UseSubmitBehavior", "false");
    panel.Controls.Add(button1 );
    

    //Contains the server code

    private void saveBtn_ServerClick(object sender, EventArgs e)
    {
       //do something if ClientClick returns true
    }
    

    //Contains the JS code for the page

    LiteralControl js = new LiteralControl();
    panel.Controls.Add(js);
    js.Text =@"   ";
    
    private string SetJsForSaveBtn()
    {
      var jsfunc = @" return CheckValidationOnClient()";
      return jsfunc ;
    }
    

提交回复
热议问题