[英]ASP.NET - onclick attribute added to Button means page cannot determine source of postback
[英]Onclick of a button run code behind and do not postback the page(refresh) asp.net
我有一個登錄下拉菜單的問題。 當我按下登錄鏈接時,下拉菜單向下滑動。 我想要的是當給出錯誤的憑證 而不回發(刷新)以便下拉菜單保持打開以供用戶輸入正確的密碼!
我試過OnClientClick =“return false;” 但這會禁用我的asp:按鈕的onclick事件。
我的代碼背后:
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);
}
我的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>
您可以基於Ajax
進行開發,以便不發布所有頁面,但只發布部分頁面。 (更新部分頁面)
<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>
您設置了不希望在ContentTemplate
之外發布的所有控件
Nota:我修復了updatemode="Conditional"
和<asp:AsyncPostBackTrigger controlid="btnLogin" eventname="Click" />
@Andreas Lympouras
在標簽中寫下這段代碼
function show() {`enter code here`
var Errormsg = document.getElementById("Error");
Errormsg.style.color = "red";
Errormsg.innerHTML = "incorrect login";
}
在代碼背后復制此代碼
string script = string.Format(@"show()");
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
現在它將調用客戶端並顯示消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.