简体   繁体   中英

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

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?

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";
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM