1

I am working in aps.net. I have this tag

ASPX file:

 <button type="submit" class="login-button" onclick="">Login</button>

corresponding .CS file:

  protected void login_Click(object sender, EventArgs e)
  {
      /* code */
  }

I want to call login_Click event. How can I call this server side event in button tag?

Satwik Nadkarny
  • 5,086
  • 2
  • 23
  • 41
  • Check this out: http://stackoverflow.com/questions/4830095/asp-net-button-onserverclick-only-works-when-onclick-isnt-defined – bp4D Jun 11 '14 at 16:51
  • 1
    Laughing at the 4 Close votes because the question is "too broad". Wow, that's a pretty bad fail in the review system. – rlb.usa Jun 11 '14 at 21:22

2 Answers2

3

Change:

<button type="submit" class="login-button" onclick="">Login</button>

To:

<asp:Button ID="login" 
            RunAt="server" 
            CssClass="login-button" 
            OnClick="login_Click" 
            Text="Login" />  
lucidgold
  • 4,432
  • 5
  • 31
  • 51
1

I think you've got sume fundamental ASP.NET mix-ups going on.

<button is a HTML tag. Any events it triggers must be javascript. Javascript will always be a client-side call. The onclick attribute refers to a javascript function.

<asp:Button is a ASP.Net control. It's OnClick event will refer to a C# method in your code-behind (your .cs file) and C# methods are always server-side. <asp:Button also has an OnClientClick attribute that refers to a javascript method.

Answer:

<asp:Button ID="someControlName" runat="server" CssClass="login-button" OnClick="login_Click" > Login </asp:Button> Note that you can also use the shorthand property Text="" to combine the closing tag.

Also, Login control and Membership

One last thing. I see that you are doing some stuff pertaining to logins? Have you read about ASP.Net's Login control specifically for this purpose? There is a lot of information that already exists pertaining to users. You should also look into ASP.Net Membership. There is a lot of information for a young ASP.Net developer to learn, but it is definitely worth the trouble.

rlb.usa
  • 14,942
  • 16
  • 80
  • 128