Stopping onclick from firing when onclientclick is false?

前端 未结 5 820
我在风中等你
我在风中等你 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:21

    I came across this issue too. Did not like to have to put the OnClientClick=return false on every linkbutton. With a simple page it just easier to use an anchor and avoid asp filling the href in for you.

    However this is not always possible. So a Simple conclusion is just to inherit the LinkButton and add a variable like AutoPostBack. if false then just override the output with the html or add the OnClientClick in. I dont really like inline tags.

    namespace My.WebControls {
        [ToolboxData("<{0}:LinkButton runat=server ID=btn></{0}:LinkButton>"), ParseChildren(true), ToolboxItem(true)]
        public class LinkButton : System.Web.UI.WebControls.LinkButton {
    
             private bool _postback = true;
             [Bindable(true), Category("Behavior"), DefaultValue(true), Description("Gets or Sets the postback click behavior")]
             public bool AutoPostBack { get { return _postback; } set { _postback = value; } }
    
    
             protected override void Render(System.Web.UI.HtmlTextWriter writer) {
                 if(!AutoPostBack){
                     this.OnClientClick = "return false";
                 }
                 base.Render(writer);
             }
        }
    }
    

    Many attributes should need to be handled in a ViewState but in this case I think we are good;

    0 讨论(0)
  • 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 =@"<script type='text/javascript'> 
    $(document).ready(function(){
     function CheckValidationOnClient(){
       if(!ValidatePage()){
        return false;
       }
       else{
        return true;
       }
     };
    });
    </script>   ";
    
    private string SetJsForSaveBtn()
    {
      var jsfunc = @" return CheckValidationOnClient()";
      return jsfunc ;
    }
    
    0 讨论(0)
  • 2020-11-30 11:33

    Sure. If you use return false within your OnClientClick it will prevent any navigation from happening. So you're code would look like:

    <asp:LinkButton runat="server" OnClientClick="if(!ValidatePage()) { return false;}" />
    
    0 讨论(0)
  • 2020-11-30 11:43

    You want to add return inside OnClientClick after a function is called. Otherwise, the button will post back even if function returns false.

    <asp:button ID="Button1" runat="server" OnClick="Button1_Click" 
        OnClientClick="return checkValidation()" Text="Submit" />
    
    <script type="text/javascript">
        function checkValidation() {
            return confirm('Everything ok?');
        }
    </script>
    
    0 讨论(0)
  • 2020-11-30 11:48

    Yes you can, In onclientClick function call use preventDefault()

    function onclientClickFun(e)
    {
      if(!IsValidationSuccess)
     {
       e.preventDefault();
     }
    
    }
    

    OR

    function onclientClickFun(e)
    {
      if(!IsValidationSuccess)
     {
       return false;
     }
    
    }
    
    0 讨论(0)
提交回复
热议问题