[英]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;" 我试过OnClientClick =“return false;” but this disables the onclick event of my asp:button. 但这会禁用我的asp:按钮的onclick事件。
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: 我的HTML代码:
<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" />
<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. 您可以基于Ajax
进行开发,以便不发布所有页面,但只发布部分页面。 (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
您设置了不希望在ContentTemplate
之外发布的所有控件
Nota : i fixed updatemode="Conditional"
and <asp:AsyncPostBackTrigger controlid="btnLogin" eventname="Click" />
Nota:我修复了updatemode="Conditional"
和<asp:AsyncPostBackTrigger controlid="btnLogin" eventname="Click" />
@Andreas Lympouras @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. 现在它将调用客户端并显示消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.