简体   繁体   English

如何在Visual Studio C#中向我自己的按钮添加按钮单击事件

[英]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). 我使用的是Visual Studio Premium2013。我试图从工具箱中自定义标准按钮控件的外观,但它没有做我想要的事情(存在边界半径和其他问题)。 So I decided to make my own button. 因此,我决定制作自己的按钮。 Its a div, styled with this css and wrapped with an anchor tag: 它是一个div,使用此CSS样式设置并用锚标记包裹:

<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 . 使用asp:LinkButton ,可以添加OnClickEvent You can read how to do it in this MSDN Article 您可以在此MSDN文章中阅读如何做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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