Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I'm working on a simple login screen with the below code:

<form id="login">
    <div class="formHeader">
      <h1>Login</h1>
    </div>
    <div class="formDiv">
      <input type="text" placeholder="Email" name="txtEmail"/>
      <div class="inputImage fa fa-user"></div>
    </div>
    <div class="formDiv">
      <input type="password" placeholder="Password" name="txtPassword"/>
      <div class="inputImage fa fa-lock"></div>
    </div>
    <div class="formDiv">
      <label id="remember">
        <input type="checkbox" name="cbxRemberMe" value="Checked"/>
        <div class="checkbox"></div><span>Remember me</span>
      </label>
    </div>
    <div class="formDiv">
      <input type="submit" value="LOGIN" runat="server" onserverclick="btnLogin_Click" onclick="btnLogin_Click" />
    </div>
    <div class="formFooter"><a class="forgot" href="#">Forgot Password</a></div>
  </form>


and back-end code

 protected void btnLogin_Click(object sender, EventArgs e) 
{

}


But the button does not call the backend code at all.

Nothing happens except the URL changes from
"http://localhost:15726/Pages/Login.aspx" to
"http://localhost:15726/Pages/Login.aspx?txtEmail=&txtPassword=&ctl00=LOGIN"

I cannot put a Runat=Server attribute in the form as I have 2 forms on this page (login and register)

any help is appreciated.

What I have tried:

1. Playing around with Runat=Server, onclick() and OnServerClick() (swapping around and changing them up)

2. Making input type=submit / input type=button

none of these work.
Posted
Updated 22-Feb-18 3:26am

1 solution

The url is updating like that because you don't have POST as the form method

<form id="login" method="POST">


However that isn't your main problem. You can have multiple forms on the page if you need to, they just can't be nested and only one can have runat=server. If your forms are not nested then you can use a normal form but you won't be able to use server-side functionality like you do with asp controls. Instead you'll need to use the page_load event and use Request.Form["controlNameHere"] to analyse the values of your form components (note it is the control's name, not the id you use to reference the control).

If your forms *are* nested then you need to get rid of all but the outer form and wrap your inner group of login controls in an asp:Panel and specify the "DefaultButton" attribute of the panel

<asp:Panel DefaultButton="btnLogin" runat="server">
    <asp:TextBox ID="txtUsername" runat="server" />
    <asp:Button ID="btnLogin" Text="Login" runat="server" />
</asp:Panel>


That creates a kind of virtual form-within-a-form where if someone presses RETURN on the username box then it will click the Login button allowing you to use btnLogin_Click. This will cause a postback containing the entire form but you just ignore the controls you're not interested in.

So to sum up either use a non-nested non-server form and use Request.Form to read the values from the form elements, or have one server form that has all controls and use Panel\DefaultButton to create a form within a form.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900