0
<asp:Button runat="server" id="LoginBtn" type="Button" Text="Login" OnClientClick="document.getElementById('jsLogin').style.display='inline-grid'" />

As the title states, the above line of code is what is present in my Master.master page.

But when I run the debugger (visual studio 2017), chrome and the application run the button with a postback, and when I examine the element, it's still showing

<input type="submit" name="ctl00$LoginBtn" value="Login" onclick="document.getElementById('jsLogin').style.display='inline-grid';" id="LoginBtn">

I have zero clue what to do.

UPDATE 7/19/2018 (20 minutes later)

reverted to an older source control:

the only change was near the top of the master.master:

<html lang="en-us">
    <form runat="server">
        <head runat="server">
            <asp:scriptmanager runat="server"></asp:scriptmanager>

instead of

<html lang="en-us">
    <head runat="server">
        <form runat="server">
            <asp:scriptmanager runat="server"></asp:scriptmanager>

and on the Default.aspx page this line:

<%@ MasterType VirtualPath="~/Master.master" %>

is not commented out.

Literally nothing else changed O.o Someone who knows more about ASP.Net than me please clue me in.

Update 7/20/2018 - 18:39 gmt

Reproduced the root cause.

<asp:textbox>

was set to required. I removed the required and created a custom javascript function. Apparently there is a problem with FormsAuthentication.Signout() when you try to clear authentication on hidden forms that have required set.

<script>
    function validation() {
        var username = document.getElementById('uNameLogin').value;
        var password = document.getElementById('uPasswordLogin').value;
        if (username == null || password == null || username == "" || password == "") 
        {
            document.getElementById('uNameLogin').value = "User Name & Password Required!";
            return false;
        }
     }
</script>

after adding that, to prevent the post back I followed the suggestion of adding return false to the OnClientClick javascript:

OnClientClick="javascript:document.getElementById('jsLogin').style.display='inline-grid'; return false;"

and it resolved the problem of the Login button causing postback.

15
  • If, while the debugger is running, I go in and manually change the type="submit" to type="button" it will run correctly. Commented Jul 20, 2018 at 3:11
  • Try setting UseSubmitBehavior to false. Commented Jul 20, 2018 at 3:13
  • Tried that, doesn't stop the button from _doPostBack It does change the inspect element to be the following: <input type="button" name="ctl00$LoginBtn" value="Login" onclick="document.getElementById('jsLogin').style.display='inline-grid';__doPostBack('ctl00$LoginBtn','')" id="LoginBtn"> Commented Jul 20, 2018 at 3:14
  • 2
    The purpose of an ASP button is to submit an event to the code-behind. If you're trying to avoid that, then why not just use a regular <input>? Commented Jul 20, 2018 at 3:16
  • 1
    Unless you're using something like an UpdatePanel, you can't have it both ways. It either submits and fires an event on the code-behind, or you prevent the submit and the code-behind does not get hit. It sounds like you want a regular <input> tied to an AJAX call to the code-behind instead. Commented Jul 20, 2018 at 3:17

1 Answer 1

1

Try following

<asp:Button runat="server" id="LoginBtn" type="Button" Text="Login" OnClientClick="javascript:document.getElementById('jsLogin').style.display='inline-grid'; return false;" />
Sign up to request clarification or add additional context in comments.

3 Comments

If I can repro the original error, I'll attempt this solution.
I was able to reproduce the original error - updating OP
after cleaning up some code that was causing the root error, this solution fixed the problem of the button postback during the OnClientClick event.

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.