How do I add a button click event to my own button in Visual Studio C#


Tags: c#,html,css,asp.net,visual-studio

Problem :

Is there a way to add a button click event to a button I create myself?

I am using Visual Studio Premium 2013. I was trying to customise the appearance of the standard button control from the toolbox but it wasnt doing what I wanted (there were issues with border-radius and other things). So I decided to make my own button. Its a div, styled with this css and wrapped with an anchor tag:

<a href="#"><div id="loginBtn">Login</div></a>                    

#loginBtn {
    width: 60px;
    height: 30px;
    margin-left: auto;
    margin-right: auto;
    background: #3B647F;
    border: 1px solid black;
    box-shadow: 3px 3px 5px gray;
    border-radius: 5px;
    text-align: center;
    line-height: 1.75;
}

But how do I add a click event to this button? I tried instead using a hyperlink control like this:

<asp:HyperLink ID="hyp1" runat="server"><div id="loginBtn">Login</div></asp:HyperLink>

But I couldnt find a way to add a click event to a hyperlink.

Anyone have any ideas?



Solution :

Use asp:LinkButton, you can add OnClickEvent. You can read how to do it in this MSDN Article

ASPX:

 <asp:LinkButton id="LinkButton1" 
           Text="Click Me" 
           OnClick="LinkButton_Click" 
           runat="server"/>

Code behind:

  void LinkButton_Click(Object sender, EventArgs e) 
  {
     Label1.Text="You clicked the link button";
  }

    CSS Howto..

    How to reference nested classes with css?

    how to set the child-class height as per the parent div using css

    Selenium: how to define css if webpage has mutiple elements with same name

    How to exclude particular class name in CSS selector?

    How to precompile scss to a single css file in webpack?

    How do I desaturate and saturate an image using CSS?

    How come one jquery script cant effect another jquery script

    How to create a vertical carousel using plain JavaScript and CSS

    How to make this text bigger?

    how can I apply a CSS stylesheet to all page views in my Firefox browser

    How to centrally align a float: left ul/li navigation menu with css?

    How to adjust the opacity of CSS background image instead of radio button with an icon overlay

    How to align navigation links after navigation menu is collapsed using bootstrap

    If there is too many menu items how to gather them into collapsed menu

    How to create the masonry effects with just bootstrap 3 grid system and css

    How to make diagonal titles on html tables

    CSS and jQuery - how to force 2 rules to work together

    How to make images go inline vs break to a new line?

    Dropdown container not showing right padding

    How to deselect a table's head and certain tds with css

    Show child elements into parent by changing its class through jquery

    How can I apply jQuery .css style to descendant div [closed]

    How to move div from bottom to top

    How do I fill up the remainder of a div with another div?

    How to delay one transition but not the other?

    How to make infinite movement of a div?

    How to make button that changes grid dimensions using JS?

    How to ensure the header floats with horizontal page expansion?

    How to make the background DIV only transparent using CSS

    how to set border in firefox