0
votes

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" onclick="btnLogin_Click" />
</div>
<div class="formFooter"><a class="forgot" href="#">Forgot Password</a></div>

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.

2
onclick is for client-side handlers only. Can you show us more of the back end to give us a better idea how this page is set up?Steve Danner
So this is webforms, right? In that case if you cannot use runat=server, then I don't think you can bind the button directly to a serverside function, but you should make a submit for the form, and then you can check request parameters in your page-load (or preload or which you use) and then call the correct method based on the input.Allan S. Hansen
It wouild perhaps be wise to do some tutorials first about the basics of webforms.VDWWD
XY Problem. It is not how ASP.NET Web Form work.Win

2 Answers

0
votes

Have you tried these two approaches?

<asp:Button runat="server" id="btnLogin" Text="LOGIN" OnClick="btnLogin_Click">

or

< button onserverclick="btnLogin_Click" runat="server" id="btnLogin">LOGIN</button>

Edit

This was supposed to be a comment and not answer. Sorry for that.

However, the entire form doesn't seem to be "webform" form compatible. If you want to be able to read the values from the form elements the form should have a runat="server" tag, too.

0
votes

Firstly, you need to specify whether it is a server control or client control. Specify runat=server to tell that it should be available for the Code-Behind file.

Finally, Use any javascript function with OnClick and any code behind function with OnServerClick

Something like this,

aspx

Plain HTML

<input type="submit" runat="server" value="LOGIN" onclick="return js_btnLogin_Click()" OnServerClick="btnLogin_Click"/>

or with ASPX Control

<asp:Button runat="server" Text="LOGIN" OnClientClick="return js_btnLogin_Click()" OnClick="btnLogin_Click">

javascript

<script type="text/javascript">

    function js_btnLogin_Click()
    {
      //return false, in case if you want to stop posting the form to the server
      return true;
    }

</script>

Code-Behind

protected void btnLogin_Click(object sender, EventArgs e) 
{

}