prevent postback of HtmlButton in C#

前端 未结 4 495
北恋
北恋 2021-01-15 16:41

I am creating HtmlButton dynamically in .cs file. Adding it to Panel using

HtmlButton b1 = new HtmlButton();
b1.Attribute.Add(\"onclick\",\"javascript:valid         


        
相关标签:
4条回答
  • 2021-01-15 16:51

    return false to prevent postback:

    b1.Attribute.Add("onclick","javascript:validateNclick(this.id);return false;");
    

    Update:
    You can also do this:

    b1.Attribute.Add("onclick","javascript:return validateNclick(this.id);");  
    

    Then in your validateNclick function, if you want to have postback, return true, if not, return false.

    0 讨论(0)
  • 2021-01-15 16:57

    if you use jQuery you can use preventDefault() to do this

    0 讨论(0)
  • 2021-01-15 17:07
    b1.Attribute.Add("onclick","return validateNclick(this.id);");
    
    function validateNclick(id){
    //...
    if(condition){
       return true
    }
    else{
      return false;
    }
    }
    
    0 讨论(0)
  • 2021-01-15 17:08

    on the javascript function return true or false and then add the return to your onclick as I show here.

        function validateNclick(me)
        {
           if(allIsOk)
           {
             return true;
           }
           else
           {
             return false
            }
        }
    

    and your attribute.

    b1.Attribute.Add("onclick","javascript:return validateNclick(this);");
    
    0 讨论(0)
提交回复
热议问题