1

I am trying to simulate a click from #btn_new_login to #btn_login. So when the user click #btn_new_login, it will simulate a click on #btn_login as well.

<button id="btn_new_login" onclick="document.getElementById('btn_login').click();">Login</button>

<asp:Button ID="btn_login" runat="server" Text="Login" OnClientClick="return checkfields()" OnClick="btn_login_Click" />
1
  • check the return value of checkfields method. It should return true for page submit Commented May 16, 2016 at 4:35

3 Answers 3

1

Use trigger via jQuery.

$('#btn_new_login').click(function(e){
    e.preventDefault();
    $(this).next('button').trigger('click');
});

remove the onclick attributes

Sign up to request clarification or add additional context in comments.

6 Comments

what do you mean by not able to get it works do you have errors in the console??
No errors. It just does not load the code behind of #btn_login which I have set breakpoints
from what i in the javascript debugger you can't create breakpoints,what debugger are you using
I am using Visual Studio. I have breakpoints on code behind. So when #btn_login get click, it will load the its code behind
does Visual Studio have a javascript debugger?, check that out
|
0
$(document).ready(function(){
    $('#btn_new_login').click(function(e){
        e.preventDefault();
        $('#' + '<%=btn_login.ClientID%>').trigger('click');
    });
});

Try this code. This should work if your checkfields method is returning true.

9 Comments

did you remove onclick attribute of btn_new_login button? Also check what value the method checkfields is returning.
what about the return value of checkfields method?
you have defined attribute OnClientClick="return checkfields()" for btn_login button. This will execute checkfields method on client side whenever btn_Login is clicked. If this method returns false, page will not submit. For once just remove this attribute and try,
Please check the value of checkfields methods through debugging in browser
btn_login.Attributes.Add("onclick","return checkfields();"); Put this code in your Page_Load event of your .aspx page. Remove the OnClientClick attrubute
|
0

Firstly, you would need to get the client id since its an asp:button

<button id="btn_new_login" onclick="document.getElementById('<%=btn_login.ClientID%>').click();">Login</button>

Also, you need to make sure your function checkfields returns true only then the server side code would be called

function checkfields(){

//do you stuff
if (all good){ 
return true;
}
else{// to prevent postback
return false;
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.