Can';t work using html textbox to login

会有一股神秘感。 提交于 2019-12-13 14:29:47

问题


Because I am using html buttons and textbox to login, I must do the code behind in the javascript in the source code in order to do the code behind. Whether I login using the correct username and password which is Admin and 123 and click the login button, or I type in nothing and click the login button, it always redirects me to ResultDetails.aspx. That means login fail. Login will pass if it redirect me to Search.aspx. What's Wrong? Even if I change .Value to .Text, it is still the same effect

MY SOURCE CODE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">

    </style>
    <link rel="stylesheet" type="text/css" href="stylesheets/loginstyle.css" />
    <script language="javascript" type="text/javascript">
// <![CDATA[




        function Button1_onclick() {

            if (txtUserName.Value == "Admin" && txtPassword.Value == "123") {
                //Login as Hardcoded User

                //Do your stuff
                window.location.assign("Search.aspx")


            }
            else {
                window.location.assign("ResultDetails.aspx")
            }           
        }

// ]]>
    </script>
</head>
<body>

<div id="wrapper">

    <form name="login-form" class="login-form" action="" method="post">

        <div class="header">
        <h1>Login Form</h1>
        <span>Fill out the form below to login to my super awesome imaginary control panel.</span>
        </div>

        <div class="content">
        <input name="username" type="text" class="input username" placeholder="Username" runat="server" id="txtUserName" />
        <div class="user-icon"></div>
        <input name="password" type="password" class="input password" placeholder="Password" runat="server" id="txtPassword" />
        <div class="pass-icon"></div>       
        </div>

        <div class="footer">
        <input type="button" name="submit" value="Login" class="button" runat="server" id="Button1" önserverclick="Button1_Click"  onclick="return Button1_onclick()" />
        </div>

    </form>

</div>
<div class="gradient"></div>
</body>
</html>

MY CODE BEHIND CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


}

回答1:


You can try below code which fetches the value from text boxes using the id in JavaScript.

function Button1_onclick() {

            if (document.getElementById('txtUserName').value == "Admin" && document.getElementById('txtPassword').value == "123") {
                //Login as Hardcoded User

                //Do your stuff
                window.location.assign("Search.aspx")


            }
            else {
                window.location.assign("ResultDetails.aspx")
            }           
        }



回答2:


You are not using asp.net server controls to access values like

txtUserName.Value

Rather, you are using Javascript on HTML Controls. So, your syntax should be like:

if (document.getElementById("txtUserName").value == "Admin" && document.getElementById("txtPassword").value == "123")



回答3:


You need to access the fields properly

e.g.

if (document.getElementById('txtUserName').value == "Admin" && document.getElementById('txtPassword').value == "123")



回答4:


If you are using Jquery lib in your code You can also do

if($.("#txtUserName").val()=="Admin" && $.("#txtPasword").val()=="123")



回答5:


if (document.getElementById("txtUserName").value == "Admin" 
    && document.getElementById("txtPassword").value == "123")


来源:https://stackoverflow.com/questions/22603037/cant-work-using-html-textbox-to-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!