简体   繁体   中英

Onclick of a button run code behind and do not postback the page(refresh) asp.net

在此输入图像描述在此输入图像描述

I have this issue of a drop down menu for my login. When I press the Log In link the drop down menu slides down. What I want is when wrong credentials are given to do not postback(refresh) so that the drop down menu will stay open for the user to type the correct password!

I have tried OnClientClick="return false;" but this disables the onclick event of my asp:button.

My code behind:

string insCmd = "SELECT ID_USER FROM users WHERE username = @LoginEmail AND password = @passwd;";
        SqlCommand Login = new SqlCommand(insCmd, con);

        Login.Parameters.AddWithValue("@LoginEmail", txtLoginEmail.Text);
        Login.Parameters.AddWithValue("@passwd", txtLoginPassword.Text);

        try
        {
            SqlDataReader dr = Login.ExecuteReader();
            SqlDataReader dr2;

            if (dr == null || !dr.HasRows)
            {
                litErrorLogin.Text = "<span>Error:Wrong Credentials</span>";

                //btnLogin.OnClientClick = "return false;";
                return;
            }

            while (dr.Read())
            {
                Session["UserID"] = dr.GetInt32(0);
            }

My html code:

<div id="bb_loginForm">
    <div class="bg_loginForm">
        <span style="font-size:11.5px;font-weight:bold">Log in to Account</span> <a class="bb_linkLoginHeaderHide" href="#" title="Hide">
            Hide</a>
        <br />
        <div class="f_username">
            <label for="txtLoginEmail" id="labelUser">
                Email
            </label>
            <br />
            <asp:TextBox class="text-input" ID="txtLoginEmail" runat="server" Height="15px" Width="130px"></asp:TextBox>
        </div>

        <div class="f_password">
            <label for="txtLoginPassoword" id="labelPassword">
                Password
            </label>
            <br />
            <asp:TextBox class="text-input" ID="txtLoginPassword" runat="server" Height="15px" Width="130px" 
                TextMode="Password"></asp:TextBox>
        </div>
        <br />
        <div class="f_login">

            <!--src="../images/GreyButton.png"   class="submit btn primary-btn" onclick="btnLogin_Click"-->
            <asp:Button class="submit google-button" ID="btnLogin" onclick="btnLogin_Click" runat="server" Text="Log in" />                                       


            <br />

            <asp:CheckBox ID="chkRememberLogin" runat="server" />
            &nbsp;
            <label class="smaller" for="chkRememberLogin" id="labelrememberusername">Remember me
            </label>
            <br />
            <a title="Forgot your login info?" href="#" class="underlineHover">Forgot your login
                info? </a>
            <br />
            <asp:Literal ID="litErrorLogin" runat="server"></asp:Literal>
        </div>
    </div>
</div>

You cna develop based on Ajax in order to don't post all your page, but you post just part of your page. (Updates partial page)

        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="btnLogin" eventname="Click" />
        </Triggers>
            <ContentTemplate>
                <asp:Literal ID="litErrorLogin" runat="server"></asp:Literal>   
                <asp:Button class="submit google-button" ID="btnLogin" onclick="btnLogin_Click" runat="server" Text="Log in" />                                       

            </ContentTemplate>
        </asp:UpdatePanel>

You set all controls that you don't want post outside of ContentTemplate

Nota : i fixed updatemode="Conditional" and <asp:AsyncPostBackTrigger controlid="btnLogin" eventname="Click" />

@Andreas Lympouras

write this code in tag

function show() {`enter code here`
            var Errormsg = document.getElementById("Error");
            Errormsg.style.color = "red";
            Errormsg.innerHTML = "incorrect login";
        }

in code behind copy this code

 string script = string.Format(@"show()");

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);

Now it will call client side and will show you the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM