Stopping onclick from firing when onclientclick is false?

匿名 (未验证) 提交于 2019-12-03 02:12:02

问题:

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 event. If the clientside check returns false, don't fire the onclick event.

Is that possible?

UPDATE:

These 2 work:

Stops the form from submitting: OnClientClick="return false;" 

Allows the form to submit: OnClientClick="return true;" 

The next 2 do not work:

// in js script tag function mycheck() {     return false; }  // in asp:button tag OnClientClick="return mycheck();" 

// in js script tag function mycheck() {     return true; }  // in asp:button tag OnClientClick="return mycheck();" 

It submits the form both times.

Why is that?

回答1:

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> 


回答2:

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;}" /> 


回答3:

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

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

OR

function onclientClickFun(e) {   if(!IsValidationSuccess)  {    return false;  }  } 


回答4:

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


回答5:

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;



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!